Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 阵列赢得';因为某种原因,我不能分类_C#_Arrays_Sorting - Fatal编程技术网

C# 阵列赢得';因为某种原因,我不能分类

C# 阵列赢得';因为某种原因,我不能分类,c#,arrays,sorting,C#,Arrays,Sorting,我正在测试一个按自定义字母顺序对数组排序的代码,但由于某些原因,每次运行程序时,ordem都不会排序 主代码 using System; using System.Collections.Generic; class MainClass { public static void Main (string[] args) { string[] myArray = {"bbjcsnmh" , "kkr"}; Array.Sort(myA

我正在测试一个按自定义字母顺序对数组排序的代码,但由于某些原因,每次运行程序时,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;
  }
}

使用系统;
使用系统集合;
类MySorter
{
公共静态整数比较字符串(字符串a、字符串b)
{
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
if(newAlphabetOrder.IndexOf((字符串)a)
newAlphabetOrder.IndexOf((字符串)b)?1:0;
}
}

也许你想这样储存它:

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;
  }
}

使用系统;
使用系统集合;
类MySorter
{
公共静态整数比较字符串(字符串a、字符串b)
{
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
int maxIterations=a.长度>b.长度?b.长度:a.长度;
对于(int i=0;inewAlphabetOrder.IndexOf(b[i]))
返回1;
}
返回0;
}
}

也许你想这样储存它:

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;
  }
}

使用系统;
使用系统集合;
类MySorter
{
公共静态整数比较字符串(字符串a、字符串b)
{
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
int maxIterations=a.长度>b.长度?b.长度:a.长度;
对于(int i=0;inewAlphabetOrder.IndexOf(b[i]))
返回1;
}
返回0;
}
}

多亏了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;
  }
}

使用系统;
使用系统集合;
类MySorter
{
公共静态整数比较字符串(字符串a、字符串b)
{
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
if(newAlphabetOrder.IndexOf(a[0])
(b[0])?1:0;
}
}

多亏了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;
  }
}

使用系统;
使用系统集合;
类MySorter
{
公共静态整数比较字符串(字符串a、字符串b)
{
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
if(newAlphabetOrder.IndexOf(a[0])
(b[0])?1:0;
}
}

您必须比较每个
字符串的对应字符:

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); 
}
公共静态整数比较字符串(字符串a、字符串b){
如果(ReferenceEquals(a,b))//a和b是相同的引用,例如,两者都为null
返回0;
if(null==a)//让null小于任何非null字符串
返回-1;
如果(null==b)
返回1;
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
对于(int i=0;i
您必须比较每个
字符串的对应字符:

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); 
}
公共静态整数比较字符串(字符串a、字符串b){
如果(ReferenceEquals(a,b))//a和b是相同的引用,例如,两者都为null
返回0;
if(null==a)//让null小于任何非null字符串
返回-1;
如果(null==b)
返回1;
var newAlphabetOrder=“kbwrqdnfxjmlvhtcgzps”;
对于(int i=0;i
您需要搜索每个字符索引的字母表,而不是整个输入字符串,因此我需要在字符数组中转换我的字母表?否。将从
IndexOf
中获得的实际索引输出:您将看到您总是得到“-1”。因此,您需要逐个查找每个输入字符串中每个字符的索引并进行比较。事实上,您可以先检查字符的相等性,然后进行查找。谢谢,我设法发现了我的错误,现在它工作正常。您需要搜索每个字符索引的字母表,不是整个输入字符串,所以我需要在字符数组中转换字母表?不是。将从
IndexOf
中获得的实际索引输出:您将看到您总是得到“-1”。因此,您需要逐个查找每个输入字符串中每个字符的索引并进行比较。事实上,您可以先检查字符的相等性,然后再进行查找。谢谢,我设法发现了我的错误,现在它工作正常。这只适用于第一个字符。看看我的解决方案,它也会检查连续的字符。它确实只会检查第一个字符@Athanasioskatarast这只会检查第一个字符。看看我的解决方案,它也会检查连续的字符。它确实只会检查第一个字符@AthanasiosKataras