可以通过 VBScript 中的自定义方法对对象的 ArrayList 进行排序吗? [英] Can an ArrayList of objects be sorted by a custom method in VBScript?

查看:37
本文介绍了可以通过 VBScript 中的自定义方法对对象的 ArrayList 进行排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先 - 我使用的语言是 VBScript,与 Windows Scripting Host 一起使用.我真的没有选择改变它并使用其他东西.

First off - the language I'm using is VBScript, used with Windows Scripting Host. I don't really have the option of changing that and using something else.

此时,我有许多包含多个字段的记录,从文件中读入.我想以多种顺序打印它们(首先,按字母顺序;其次,按位置分组,然后在位置内按字母顺序排序;等等).记录存储在一个 ArrayList 中.

At this point, I have a number of records with multiple fields, read in from a file. I want to print them out in a number of orderings (first, in alphabetical order; second, grouped by location and then sorted alphabetically within locations; etc.). The records are stored in an ArrayList.

如果我使用的是 Visual Basic 而不是 VBScript,我知道我可以创建实现 iComparer 接口的类,然后在我想按该顺序排序时使用适当类的对象调用 ArrayList 的 Sort 函数(使用 Alphabetical 对象进行第一次排序,使用 LocationThenAlphabetical 对象进行第二次排序等)

If I was using Visual Basic rather than VBScript, I know that I could create classes which implemented the iComparer interface, and then call the Sort function of the ArrayList with an object of the appropriate class when I wanted to sort in that order (use the Alphabetical object for the first sort, the LocationThenAlphabetical object for the second sort, etc.)

然而,我一直在疯狂地试图找到一种方法来做到这一点或在 VBScript 中做任何类似的事情,我实际上可以使用这种语言.我在谷歌上搜索了无数VBScript"、ArrayList"、排序"、自定义排序"、比较"、自定义比较"、实现"、接口"的组合——但每当我发现与 VBScript 相关的结果时,它们与如何进行自定义排序无关,每当我发现与自定义排序相关的结果时,它们都不适用于 VBScript,即使我在搜索中明确指定了这一点.

However, I have been going crazy trying to find a way to do that or anything similar in VBScript, the language I actually have available to work with. I've Googled endless combinations of "VBScript", "ArrayList", "sort", "custom sort", "comparison", "custom comparison", "implements", "interface" - but whenever I find results relevant for VBScript, they aren't about how to do a custom sort, and whenever I find results relevant to custom sorting, they aren't applicable to VBScript, even if that was clearly specified in my search.

如果可能的话,我宁愿以正确的方式做到这一点.如果根本不可能在 VBScript 中进行自定义排序,我可以看到一个可能的解决方法,但它会增加很多复杂性并使用比我认为自定义排序更多的内存.

I would rather do this the right way, if that's possible. If it's simply not possible to do a custom sort in VBScript, I can see a possible work-around, but it would add a lot of complexity and use a lot more memory than I think a custom sort would do.

推荐答案

鉴于您必须将 System.Collections.IComparer 传递给 .Sort 方法并且您无法在 VBScript 中创建 .NET 对象,答案是否定的.

Given that you'd have to pass a System.Collections.IComparer to the .Sort method and that you can't create .NET objects in VBScript, the answer is no.

这篇关于可以通过 VBScript 中的自定义方法对对象的 ArrayList 进行排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆