【问题标题】:Array won't sort for some reason由于某种原因,数组不会排序
【发布时间】:2021-06-04 05:12:38
【问题描述】:

我正在测试以自定义字母顺序对数组进行排序的代码,但由于某种原因,每次我运行程序时,ordem 都不会排序

主代码

using System;
using System.Collections.Generic;

class MainClass {
  public static void Main (string[] args) {
    string[] myArray = {"bbjcsnmh" , "kkr"};

    Array.Sort(myArray, MySorter.CompareStrings);

    foreach(string s in myArray)
    {
      Console.WriteLine(s); 
    }
  }
}

自定义排序器

using System;
using System.Collections;

class MySorter
{
  public static int CompareStrings(string a, string b)
  {
    var newAlphabetOrder = "kbwrqdnfxjmlvhtcgzps";
    if(newAlphabetOrder.IndexOf((string) a) < newAlphabetOrder.IndexOf((string) b))
      return -1;

    return newAlphabetOrder.IndexOf((string) a) > 
    newAlphabetOrder.IndexOf((string) b) ? 1 : 0;
  }
}

【问题讨论】:

  • 您需要搜索每个字符的索引的字母表,而不是整个输入字符串
  • 所以我需要将我的字母转换成一个字符数组?
  • 没有。输出您从IndexOf 获得的实际索引:您会看到您总是得到“-1”。所以你需要一一查找每个输入字符串中每个字符的索引并进行比较。
  • 实际上你可以先检查字符是否相等,然后再进行查找。
  • 谢谢,我已经看到我的错误了,现在它可以正常工作了

标签: c# arrays sorting


【解决方案1】:

也许你想这样处理它:

using System;
using System.Collections;

class MySorter
{
  public static int CompareStrings(string a, string b)
  {
    var newAlphabetOrder = "kbwrqdnfxjmlvhtcgzps";
    int maxIterations = a.Length > b.Length ? b.Length : a.Length;
    for (int i = 0; i < maxIterations; i++) {
        if(newAlphabetOrder.IndexOf(a[i]) < newAlphabetOrder.IndexOf(b[i]))
             return -1;
        if(newAlphabetOrder.IndexOf(a[i]) > newAlphabetOrder.IndexOf(b[i]))
             return 1;
    }

    return 0;
  }
}

Net Fiddle - PoC

【讨论】:

    【解决方案2】:

    你要比较每个string的对应字符:

    public static int CompareStrings(string a, string b) {
      if (ReferenceEquals(a, b)) // a and b are same references, e.g. both are null 
        return 0;
      if (null == a) // let null be smaller then any not-null string 
        return -1;
      if (null == b)
        return 1;
    
      var newAlphabetOrder = "kbwrqdnfxjmlvhtcgzps";
    
      for (int i = 0; i < Math.Min(a.Length, b.Length); ++i) {
        int indexA = newAlphabetOrder(a[i]);
        int indexB = newAlphabetOrder(b[i]);
    
        //TODO: you may want to add some logic if a[i] or b[i] is not in alphabet
        int compare = indexA.CompareTo(indexB);
    
        if (compare != 0)
          return compare; 
      }  
    
      // in case of same prefix, e.g. "kbw" and "kbwjjj" shorter string is smaller
      return a.Length.CompareTo(b.Length); 
    }
    

    【讨论】:

      【解决方案3】:

      感谢 Fildor,我设法找出了我错的地方,在这个新的排序器中,我将 ((char) a) 更改为“a[0]”,因此它将检查数组所有交互的第一个字母.

      using System;
      using System.Collections;
      
      class MySorter
      {
        public static int CompareStrings(string a, string b)
        {
          var newAlphabetOrder= "kbwrqdnfxjmlvhtcgzps";
          if(newAlphabetOrder.IndexOf(a[0]) < newAlphabetOrder.IndexOf(b[0]))
            return -1;
      
          return newAlphabetOrder.IndexOf(a[0]) > 
          newAlphabetOrder.IndexOf(b[0]) ? 1 : 0;
        }
      }
      
      

      【讨论】:

      • 这仅适用于第一个字符。看看我的解决方案,它也检查连续的。
      • 确实它只会检查第一个字符@AthanasiosKataras
      猜你喜欢
      • 1970-01-01
      • 2021-08-23
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多