【问题标题】:How to sort an array of non-english (Slovanian) words in C#?如何在 C# 中对一组非英语(斯洛文尼亚语)单词进行排序?
【发布时间】:2016-08-03 09:09:10
【问题描述】:

我有一个由斯洛文尼亚语字符组成的字符串,例如 (žiga, špela, črt, ...),我需要按字母顺序对这个字符串进行排序,我想只使用循环而不使用任何内置函数。 NET 喜欢array.Sort()

static string[] SortByName(string[] fullname)
{
    char[] abc =
    {
        'a', 'b', 'c', 'č', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 'š','t', 'u', 'v', 'z', 'ž'
    };

    int counter = 0;
    for (int i = 0; i<abc.Length;i++)
    {
        for (int j = 0; j < fullname[counter].Length; j++)
        {
            // I still cant figure out what i can do here 
        }
        counter++;
    }
}

【问题讨论】:

  • 创建Comparer 并使用Sort 有什么问题?
  • @Omar 尝试使用文化Array.Sort(abc, StringComparer.InvariantCulture);
  • 这是一个赋值,只允许使用循环而不是更多

标签: c# arrays sorting


【解决方案1】:

在您的程序中,char 数组 abc 按顺序存储所有斯洛文尼亚字母。您可以将此数组用作对所有斯洛文尼亚语字母的排名的参考,以比较斯洛文尼亚语单词。在下面的代码中,我定义了一个方法CompareSl() 来比较斯洛文尼亚语单词,就像String 类的Compare() 方法比较英语单词一样。 (方法index()返回一个字符在数组abc中的索引。)

String 类的 Compare() 方法接受两个字符串 s1s2 作为参数,并且 ♦ 如果字符串相等则返回 0
♦ 如果 s1 > s2
则返回 (+)ve ♦ 如果 s1

则返回 (-)ve
using System;

class Sl_Sort{    

    static void Main(){
        string[] words = new string[]{"žiga", "špela", "črt"};

        Console.WriteLine("Unsorted array is");
        foreach(String st in words)
            Console.Write(st+" , ");
        //do selection sort to sort in ascending order

        for(int i=0; i<words.Length-1;i++){
            int min = i;
            for(int j=i+1; j<words.Length;j++){
                if(CompareSl(words[min], words[j]) > 0)
                    min  = j;
            }
            string temp = words[i];  
            words[i] = words[min]; 
            words[min] = temp;
        }
        Console.WriteLine("\nSorted array is");
        foreach(String st in words)
            Console.Write(st+" , ");
        Console.ReadKey(); //waits till user presses a key before terminating
    }

    public static int CompareSl(string s1, string s2){
        if(s1.Length == s2.Length){
            for(int i=0; i<s1.Length; i++){
                if(s1[i] != s2[i])
                    return index(s1[i]) - index(s2[i]);
            }
        }
        else{
            String s = s1.Length<s2.Length?s1:s2;
            for(int i=0; i<s.Length; i++){
                if(s1[i] != s2[i])
                    return index(s1[i]) - index(s2[i]);
            }
            if(s == s1)
            return -1;
            else
            return 1;
        }
        return 0;
    }

    private static int index(char c){
        char[] abc = { 'a', 'b', 'c', 'č', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 'š', 't', 'u', 'v', 'z', 'ž' };
        for(int i=0; i<abc.Length; i++){
            if(abc[i]==c)
            return i;
        }
        return -1;
    }
}

输出:

Unsorted array is
ziga , spela , crt ,

Sorted array is
crt , spela , ziga  

注意:字符 žšč 分别转换为 zsc,因为我运行代码的平台未设置为 UTF-8 或 @ 987654336@,支持斯洛文尼亚语,但ANSI,不支持斯洛文尼亚语。确保您的系统支持 Slovene 以获得正确的输出。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    也许你可以这样做:

     private char[] charList = { 'a', 'b', 'c', 'č', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 'š', 't', 'u', 'v', 'z', 'ž' };
    public string[] Sort(string inputStr)
    {
        string[] sortedChars = new string[inputStr.Length];
        int index = 0;
        for (int i = 0; i < charList.Length; i++)
        {
            for (int u = 0; u < inputStr.Length; u++)
            {
                sortedChars[index] = inputStr[u];
                index++;
            }
            if (index == inputStr.Length)
                return sortedChars;   
        }   
        return sortedChars;
    }   
    

    我没有测试过代码,所以不确定它是否有效,但它的作用是: 遍历字符,因此它在 inputString 的每个字符上搜索“a”,如果它在那里,它将被添加到排序的字符串 [],然后当它完成第一个字符时,下一个......等等。

    可能有一些错误,也许你可以改进代码。

    【讨论】:

    • 它没有用,我试图修复代码但仍然无法正常工作
    • 哇,我想我完全误解了你的问题。我正在更改代码,但代码是对每个字符串上的字符进行排序,如下所示:ziga->agiž。我现在觉得很愚蠢:P
    猜你喜欢
    • 2012-02-01
    • 2013-11-24
    • 2011-10-19
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多