<bdo id='tskrk'></bdo><ul id='tskrk'></ul>
<i id='tskrk'><tr id='tskrk'><dt id='tskrk'><q id='tskrk'><span id='tskrk'><b id='tskrk'><form id='tskrk'><ins id='tskrk'></ins><ul id='tskrk'></ul><sub id='tskrk'></sub></form><legend id='tskrk'></legend><bdo id='tskrk'><pre id='tskrk'><center id='tskrk'></center></pre></bdo></b><th id='tskrk'></th></span></q></dt></tr></i><div id='tskrk'><tfoot id='tskrk'></tfoot><dl id='tskrk'><fieldset id='tskrk'></fieldset></dl></div>

    1. <small id='tskrk'></small><noframes id='tskrk'>

        <tfoot id='tskrk'></tfoot>
        <legend id='tskrk'><style id='tskrk'><dir id='tskrk'><q id='tskrk'></q></dir></style></legend>

        对混合数字和字符串进行排序

        Sorting mixed numbers and strings(对混合数字和字符串进行排序)
        • <i id='KACGL'><tr id='KACGL'><dt id='KACGL'><q id='KACGL'><span id='KACGL'><b id='KACGL'><form id='KACGL'><ins id='KACGL'></ins><ul id='KACGL'></ul><sub id='KACGL'></sub></form><legend id='KACGL'></legend><bdo id='KACGL'><pre id='KACGL'><center id='KACGL'></center></pre></bdo></b><th id='KACGL'></th></span></q></dt></tr></i><div id='KACGL'><tfoot id='KACGL'></tfoot><dl id='KACGL'><fieldset id='KACGL'></fieldset></dl></div>

          <tfoot id='KACGL'></tfoot>

              <small id='KACGL'></small><noframes id='KACGL'>

                  <tbody id='KACGL'></tbody>

                <legend id='KACGL'><style id='KACGL'><dir id='KACGL'><q id='KACGL'></q></dir></style></legend>
                  <bdo id='KACGL'></bdo><ul id='KACGL'></ul>
                  本文介绍了对混合数字和字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个字符串列表,其中可以包含一个字母或一个 int 的字符串表示形式(最多 2 位数字).它们需要按字母顺序或(当它实际上是一个 int 时)按它所代表的数值排序.

                  I have a list of strings that can contain a letter or a string representation of an int (max 2 digits). They need to be sorted either alphabetically or (when it is actually an int) on the numerical value it represents.

                  例子:

                  IList<string> input = new List<string>()
                      {"a", 1.ToString(), 2.ToString(), "b", 10.ToString()};
                  
                  input.OrderBy(s=>s)
                    // 1
                    // 10
                    // 2
                    // a
                    // b
                  

                  我想要的是

                    // 1
                    // 2
                    // 10
                    // a
                    // b
                  

                  我有一些想法涉及格式化它并尝试解析它,然后如果它是一个成功的 tryparse 用我自己的自定义 stringformatter 格式化它以使其具有前面的零.我希望有更简单、更高效的东西.

                  I have some idea involving formatting it with trying to parse it, then if it is a successfull tryparse to format it with my own custom stringformatter to make it have preceding zeros. I'm hoping for something more simple and performant.

                  编辑
                  我最终制作了一个 IComparer,我将其转储到我的 Utils 库中以供以后使用.
                  当我这样做的时候,我也加入了双打.

                  Edit
                  I ended up making an IComparer I dumped in my Utils library for later use.
                  While I was at it I threw doubles in the mix too.

                  public class MixedNumbersAndStringsComparer : IComparer<string> {
                      public int Compare(string x, string y) {
                          double xVal, yVal;
                  
                          if(double.TryParse(x, out xVal) && double.TryParse(y, out yVal))
                              return xVal.CompareTo(yVal);
                          else 
                              return string.Compare(x, y);
                      }
                  }
                  
                  //Tested on int vs int, double vs double, int vs double, string vs int, string vs doubl, string vs string.
                  //Not gonna put those here
                  [TestMethod]
                  public void RealWorldTest()
                  {
                      List<string> input = new List<string>() { "a", "1", "2,0", "b", "10" };
                      List<string> expected = new List<string>() { "1", "2,0", "10", "a", "b" };
                      input.Sort(new MixedNumbersAndStringsComparer());
                      CollectionAssert.AreEquivalent(expected, input);
                  }
                  

                  推荐答案

                  也许您可以采用更通用的方法并使用 自然排序算法如C#实现这里.

                  Perhaps you could go with a more generic approach and use a natural sorting algorithm such as the C# implementation here.

                  这篇关于对混合数字和字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <tbody id='c0D6N'></tbody>

                  <i id='c0D6N'><tr id='c0D6N'><dt id='c0D6N'><q id='c0D6N'><span id='c0D6N'><b id='c0D6N'><form id='c0D6N'><ins id='c0D6N'></ins><ul id='c0D6N'></ul><sub id='c0D6N'></sub></form><legend id='c0D6N'></legend><bdo id='c0D6N'><pre id='c0D6N'><center id='c0D6N'></center></pre></bdo></b><th id='c0D6N'></th></span></q></dt></tr></i><div id='c0D6N'><tfoot id='c0D6N'></tfoot><dl id='c0D6N'><fieldset id='c0D6N'></fieldset></dl></div>

                      <small id='c0D6N'></small><noframes id='c0D6N'>

                          <bdo id='c0D6N'></bdo><ul id='c0D6N'></ul>
                        • <legend id='c0D6N'><style id='c0D6N'><dir id='c0D6N'><q id='c0D6N'></q></dir></style></legend>
                          <tfoot id='c0D6N'></tfoot>