如何自定义字符串的排序顺序 [英] How to customize sort order of strings

查看:376
本文介绍了如何自定义字符串的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的使用字符串数组。考虑这个例子:

I am completely new to using string arrays. Consider this example:

我有一个文本框中输入以下值:

I have the following values in a text box:

bufallo@2000
lice@20
cell@1
rat@150
cow@10000

当我对它们进行排序,他们是按字母顺序排序,如上面的列表中。不过,我需要他们在使用@符号后面的整数值降序排序。因此,例如,我想上面的列表进行排序如下:

When I sort them, they are sorted alphabetically, as in the list above. However, I need them to sort in descending order using the integer value that follows the @ symbol. So, for instance, I want the above list to be sorted like this:

cow@10000
bufallo@2000
rat@150
lice@20
cell@1

我对如何在他们那样的递减顺序排列不知道。

I have no idea on how to arrange them in descending order like that.

推荐答案

您想通过字符串的数字部分订购?无需正则表达式。

You want to order by the numeric part of the string? No need for Regex.

您可以使用 String.Split Enumerable.OrderByDescending

Dim number As Int32 = Int32.MinValue
Dim orderedLines = From line In TextBox1.Lines
                   Let parts = line.Split("@"c)
                   Let numericPart = parts.Last()
                   Let success = Int32.TryParse(numericPart, number)
                   Select LineInfo = New With {line, number}
                   Order By LineInfo.number Descending
                   Select LineInfo.line
' if you want to reassign it to the TextBox:
TextBox1.Lines = orderedLines.ToArray()

这篇关于如何自定义字符串的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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