[英]How to sort an array of non-english (Slovanian) words in C#?
I have a string composes of Slovenian characters such as (žiga, špela, črt, ...) and i need to sort this string alphabetically, the problem i want to do that using loops only without using any builtin function in .NET like array.Sort()
. 我有一个斯洛文尼亚字符组成的字符串,例如(žiga,špela,črt,...),我需要按字母顺序对字符串进行排序,这个问题我只想使用循环来实现,而无需在.NET之类的
array.Sort()
使用任何内置函数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++;
}
}
In your program, the char
array abc
stores all the Slovenian alphabets in order. 在您的程序中,
char
数组abc
按顺序存储所有斯洛文尼亚字母。 You can use this array as a reference to the ranks of all the Slovene alphabets to compare the Slovene words. 您可以使用此数组作为所有斯洛文尼亚字母排名的参考,以比较斯洛文尼亚单词。 In the code below, I have defined a method
CompareSl()
to compare Slovene words, just like the Compare()
method of the String
class compares English words. 在下面的代码中,我定义了一个
CompareSl()
方法来比较斯洛文尼亚单词,就像String
类的Compare()
方法比较英语单词一样。 (The method index()
returns the index of a character in the array abc.) (方法
index()
返回数组abc中字符的索引。)
The Compare() method of the String class takes two strings s1 and s2 as arguments and
String类的Compare()方法将两个字符串s1和s2作为参数,
♦ Returns 0 if the strings are equal•如果字符串相等,则返回0
♦ Returns (+)ve if s1 > s2♦如果s1> s2返回(+)ve
♦ Returns (-)ve if s1 < s2♦如果s1 <s2返回(-)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;
}
}
OUTPUT: 输出:
Unsorted array is
ziga , spela , crt ,
Sorted array is
crt , spela , ziga
Note: The characters ž
, š
and č
got converted to z
, s
and c
respectively because the platform I ran the code on was not set to UTF-8
or Unicode
, which support Slovene, but ANSI
, which does not support Slovene. 注意:字符
ž
, š
和č
分别转换为z
, s
和c
因为我在其上运行代码的平台未设置为UTF-8
或Unicode
(支持Slovene,但设置为ANSI
,但不支持Slovene)。 Make sure your system supports Slovene to get the correct output. 确保您的系统支持Slovene以获取正确的输出。
Hope this helps. 希望这可以帮助。
Maybe you could do something like this: 也许您可以执行以下操作:
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;
}
I haven't tested the code, so not sure it works, but what it does is: Iterate trough the chars, so it searches for "a" on each char of the inputString, if it's there it will be added to the sorted string[], then when it finishes with the first char, the next...etc. 我还没有测试代码,所以不确定它是否有效,但是它的作用是:遍历char,因此它在inputString的每个char上搜索“ a”,如果有,它将被添加到已排序的字符串中[],然后以第一个字符结束时,再输入下一个...等。
Probably there's some error, maybe you can improve the code. 可能有一些错误,也许您可以改进代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.