如何自定义字符串的排序顺序

编程入门 行业动态 更新时间:2024-10-12 01:29:02
本文介绍了如何自定义字符串的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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()

更多推荐

如何自定义字符串的排序顺序

本文发布于:2023-11-10 14:21:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字符串   顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!