数组列表排序
我有一个包含大量字符串的 ArrayList。它需要根据三个字段(本质上是三个子字符串)进行就地排序:Name、Age 和 Amt。 Age 是第一个子字符串(位置 0-3),Name 是第二个子字符串(3-6),Amt 是最后一个子字符串(6-10) 。这些参数的排序顺序非常重要,如下所示:
首先按名称升序排序,然后按升序排序Age(实际上在子字符串中排在前面),然后按 Amt 进行降序排序。就是这样。
我有一个类
public class ArrComparer : IComparer
{
public int Compare(object x, object y)
{
string left = x.ToString();
string right = y.ToString();
string lhs = left.Substring(3, 6);
string rhs = right.Substring(3, 6);
return lhs.CompareTo(rhs);
}
}
,我用它来仅根据一个字段进行排序 - 通过调用“名称”
RecordList.Sort(new ArrComparer());
这可以让我根据该一个字段进行正确排序。问题是如何修改此代码以允许我按正确的顺序并使用正确的升序/降序模式基于所有三个一次进行排序?
任何代码或提示将不胜感激。 (顺便说一句,如果您想知道在该项目中不可以选择使用通用 List
)。
I have an ArrayList that contains a large number of strings. It needs to be sorted in place based on three fields (essentially three substrings) which are Name, Age and Amt. Age is the first substring (position 0-3), Name is second (3-6) and Amt is last (6-10). The order in which these parameters are to be sorted is very important and is as follows:
First perform ascending sort by Name THEN do ascending sort by Age (which actually comes earlier in the substring) and THEN do descending sort by Amt. That's it.
I have this class
public class ArrComparer : IComparer
{
public int Compare(object x, object y)
{
string left = x.ToString();
string right = y.ToString();
string lhs = left.Substring(3, 6);
string rhs = right.Substring(3, 6);
return lhs.CompareTo(rhs);
}
}
which I use to sort based on just one field - Name by invoking
RecordList.Sort(new ArrComparer());
This lets me sort correctly based on that one field. The question is how can I modify this code to allow me to sort based on all three AT ONCE, in the right order and using proper asc/desc mode?
Any code or tips would be greatly appreciated. (By the way, in case you are wondering using generic List<T>
is not an option in this project).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ArrayList 仍然实现 IEnumerable,这意味着您可以在 linq 中使用简单的 orderby() 和 thenby() 扩展:
表达这一点的其他方法包括构建更复杂的 .OrderBy() 或使用匿名类型将字符串组成为对象:
I喜欢这个选项,因为它让你开始用对象的方式思考。正确发挥你的牌,你可以跳过最后一个 .Select() 投影来保留对象,而不是返回字符串,这将节省稍后必须重新进行所有解析的工作。
如果这些不是一个选项(可能出于同样的原因您不能使用 List),则可以轻松修改现有的比较方法,如下所示:
ArrayList still implements IEnumerable, meaning you can use the simple orderby() and thenby() extensions in linq:
Other ways to express this include building a more complicated .OrderBy() or using an anonymous type to compose your string as an object:
I like that option because it sets you up to start thinking in terms objects. Play your cards right and you can skip that last .Select() projection to keep the objects rather than going back to strings, which will save the work of having to do all that parsing over again later.
If these aren't an option (possibly for the same reason you can't use List<T>), it's easy to modify your existing compare method like so:
您可以逐个比较:
You can compare part by part:
如果您需要 IComparer,请尝试以下操作:
If you need an IComparer, try something like:
我建议将您的记录存储在一个对象中,并使它们具有可比性。
为了使用您当前使用的相同方法比较所有三个字段,您只需提取所有三个数据并进行完整比较。
I would recommend storing your records in an object, and make those comparable instead.
In order to compare all three fields using the same method you are currently using you simply need to extract all three pieces of data and do a full comparison.
您可以将 ArrCompare 与 if 语句(例如 if(rhs == lhs) compere)与字符串的其他部分一起使用。
Accen Decend 是 return -1 或 1 的满足者
you could expend your ArrCompare with if statements like if(rhs == lhs) compere with other part of string.
Accen deccend is meeter of return -1 or 1