简体   繁体   English

2D字符串数组(带有局部土耳其语字符)在J​​ava中排序

[英]2D string array(with local Turkish characters) sorting in java

i have a 2D array of string it is like: 我有一个二维的字符串数组,就像:

gsdfsdf | awfdsf 
asdasd  | sjd    
çsadsfd | kdjfkj 
bsdmfbs | skfjef 
ıfjdhsb | döjfn  
zewkjf  | skjfkj 
iadsa   | dfjdkj 

i want to sort it according to first column as you see it has local(Turkish) characters, and after sorting it must be like: 我想根据第一列对其进行排序,因为您看到它具有本地(土耳其语)字符,并且在排序之后必须像:

asdasd  | sjd    
bsdmfbs | skfjef 
çsadsfd | kdjfkj 
gsdfsdf | awfdsf 
ıfjdhsb | döjfn  
iadsa   | dfjdkj 
zewkjf  | skjfkj 

any idea, tutorial or advice? 有什么想法,教程或建议吗?

Not a full working solution, but a hint. 不是一个完整的解决方案,而是一个提示。

Java provides a Collator class for locale sensitive string comparison. Java提供了一个Collator类,用于语言环境敏感的字符串比较。

Sample (with guava): 样品(番石榴):

Foo

package com.stackoverflow.so13895464;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Foo {
    public static String sortAndOutput(final String in)
    {
        final List<String> split = Lists.newArrayList(Splitter.on('\n').split(in));
        Collections.sort(split, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2)
            {
               final Collator collator = Collator.getInstance(Locale.forLanguageTag("tr_TR"));
               // XXX: toUpperCase to avoid a dotless i problem (was at the end)
               return collator.compare(o1.toUpperCase(), o2.toUpperCase()); 
            }
        });
        return Joiner.on('\n').join(split);
    }
}

FooTest FooTest

package com.stackoverflow.so13895464;

import org.junit.Test;

import static org.junit.Assert.*;

public class FooTest {
    @Test
    public void testSort()
    {
        final String in = "gsdfsdf | awfdsf\nasdasdsjd\nçsadsfd | kdjfkj\nbsdmfbs | skfjef\n" +  
            "ıfjdhsb | döjfn\nzewkjf  | skjfkj\niadsa   | dfjdkj";
        final String exp = "asdasd  | sjd\nbsdmfbs | skfjef\nçsadsfd | kdjfkj\ngsdfsdf | awfdsf\n" +
            "ıfjdhsb | döjfn\niadsa   | dfjdkj\nzewkjf  | skjfkj";
        assertEquals(exp, Foo.sortAndOutput(in));
    }
}

Comparator should be improved for null checking,.. etc. but this should work! 应该改进比较器以进行null检查等。但这应该可以工作!

final String alphabet = "0123456789AaBbCcÇçDdEeFfGgĞğHhIıİiJjKkLlMmNnOoÖöPpQqRrSsŞşTtUuÜüVvWwXxYyZz";
SortedMap<String,String> map = new TreeMap(new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            int n = Math.min(o1.length(), o2.length());
            int i=0;
            while((i < n) && (o1.charAt(i) == o2.charAt(i))){
                i++;
            }
            if(i==n){
                return 0;
            }
            if(alphabet.indexOf(o1.charAt(i)) < alphabet.indexOf(o2.charAt(i))){
                return -1;
            }
            return 1;
        }
    });

Rather than use a 2D array of string I'd use a TreeMap . 与其使用二维字符串数组,不如使用TreeMap This will give you ability to sort on the key of the TreeMap. 这使您能够对TreeMap的键进行排序。 By default this is naturally ordered so it will be based alphabetically which is what you need although I'm not sure where alphabetically you want the Turkish characters. 默认情况下,这是自然排序的,因此它将根据您的需要按字母顺序排列,尽管我不确定您希望土耳其语字母按字母顺序排列。

TreeMap tm = new TreeMap(); 
// Put elements to the map 
tm.put("Zoe Doe", "First entry"); 
tm.put("Alex Smith", "Second entry"); 
tm.put("Gareth Baker", "Third entry");
// Get a set of the entries 
Set set = tm.entrySet(); 
// Get an iterator 
Iterator i = set.iterator(); 
// Display elements 
while(i.hasNext()) { 
Map.Entry me = (Map.Entry)i.next(); 
System.out.print(me.getKey() + ": "); 
System.out.println(me.getValue()); 
} 

This should output the values of the TreeMap in alphabetical order: 这应该按字母顺序输出TreeMap的值:

  • Alex Smith 亚历克斯·史密斯
  • Gareth Baker 加雷斯·贝克
  • Zoe Doe 佐伊·多伊

If you need to change this then you can implement comparable and pass it in on the constructor of the TreeMap: 如果需要更改此设置,则可以实现可比并将其传递给TreeMap的构造函数:

TreeMap tm = new TreeMap(new Comparator<Foo>()
             {
                 public int compare(Foo f1, Foo f2)
                 {
                     return f1.toString().compareTo(f2.toString());
                 }        
             });

You'll have to put the logic you want in the compare method above. 您必须将所需的逻辑放入上面的compare方法中。 As it seems you may need a different implementation than the standard alphabetical ordering. 看起来您可能需要与标准字母顺序不同的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM