Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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
Java 二维字符串数组中基于整数的排序_Java_Arrays_Sorting - Fatal编程技术网

Java 二维字符串数组中基于整数的排序

Java 二维字符串数组中基于整数的排序,java,arrays,sorting,Java,Arrays,Sorting,我有一个二维字符串数组 2 10 BakerSarah D 2 11 SmothersSally A 2 12 SillySall C 2 13 Viper B 2 5 LouieChef B 2 6 Lawson C 每列都是字符串。现在我想在第二列对其进行排序。我试过这个密码 void sortarray(final int index){ Arrays.sort(data, new Comparator<Object[]>(){

我有一个二维字符串数组

2 10 BakerSarah D 
2 11 SmothersSally A 
2 12 SillySall C 
2 13 Viper B 
2 5 LouieChef B 
2 6 Lawson C  
每列都是字符串。现在我想在第二列对其进行排序。我试过这个密码

void sortarray(final int index){
        Arrays.sort(data, new Comparator<Object[]>(){
            @Override
            public int compare(Object[] o1,  Object[] o2) {
                String[] a = (String[])o1;
                String[] b = (String[])o1;
                return a[index].compareTo(b[index]);
            }
        });

    }   
按顺序。为什么会这样?? 如何将其更改为排序

2 5 LouieChef B 
2 6 Lawson C   
2 10 BakerSarah D 
2 11 SmothersSally A 
2 12 SillySall C 
2 13 Viper B 

当比较返回0(即它们相等)时,您需要对另一个索引进行比较。我已经更新了您的代码,使其具有这个新的索引-index2

void sortarray(final int index, final int index2){
    Arrays.sort(data, new Comparator<Object[]>(){
        @Override
        public int compare(Object[] o1,  Object[] o2) {
            String[] a = (String[])o1;
            String[] b = (String[])o1;
            Integer i = a[index].compareTo(b[index]);
            if (i == 0) {
               return a[index2].compareTo(b[index2]);
            } 
            return i;
        }
    });

}   
void sortarray(最终整数索引,最终整数索引){
sort(数据,新的比较器(){
@凌驾
公共整数比较(对象[]o1,对象[]o2){
字符串[]a=(字符串[])o1;
字符串[]b=(字符串[])o1;
整数i=a[index]。与(b[index])比较;
如果(i==0){
返回a[index2]。与(b[index2])进行比较;
} 
返回i;
}
});
}   
我假设(可能是错误的)您希望对我的数据按第一列排序,然后按第二列排序。如果只是第二个,请尝试@x4rf41所说的,并执行Integer.valueOf将字符串转换为整数


就我个人而言,我会创建一个对象并在此基础上实现,这样您就可以以更面向对象的方式进行排序。

字符串具有自然的词典顺序。这意味着“10”在“5”之前。整数具有自然的数字顺序。因此,您应该将字符串转换为数字并比较数字:

Arrays.sort(data, new Comparator<Object[]>(){
    @Override
    public int compare(Object[] o1,  Object[] o2) {
        String[] a = (String[])o1;
        String[] b = (String[])o1;
        if (index == 2) { // lexicographic order
            return a[index].compareTo(b[index]);
        }
        else { // numeric order
            int left = Integer.parseInt(a[index]);
            int right = Integer.parseInt(b[index]);
            return Integer.compare(left, right);
        }
    }
});

Java是一种面向对象语言。使用对象。

如果需要整数排序,但您的值仍然是字符串,则需要将其转换为整数,并比较需要将字符串转换为整数的整数,然后排序将起作用。Index2是您要排序的第二个索引。在本例中,是包含5,6,10,….的列,但索引参数本身并不能回答问题。
Arrays.sort(data, new Comparator<Object[]>(){
    @Override
    public int compare(Object[] o1,  Object[] o2) {
        String[] a = (String[])o1;
        String[] b = (String[])o1;
        if (index == 2) { // lexicographic order
            return a[index].compareTo(b[index]);
        }
        else { // numeric order
            int left = Integer.parseInt(a[index]);
            int right = Integer.parseInt(b[index]);
            return Integer.compare(left, right);
        }
    }
});
public class Row { // choose a better name
    private int field1; // choose a better name
    private int field1; // choose a better name
    private String name;

    // constructor and getters omitted
}