字符串和整数的CompareTo/Sorting二维数组 [英] CompareTo/Sorting 2D array of Strings AND Integers
问题描述
好吧,所以我现在想出了如何通过字符串和整数对2d数组进行排序,但是我不确定如何将它们组合在一起.
Ok, so I just now figured out how to sort a 2d array via string and integers, however I am not sure how to do combine them.
我有一个二维用户数组,每一行都有一个用户数据.数据分别是列中的字符串或整数,因此如果我有多种比较方法,例如按名称排序或按电话号码排序,那么如何实现初始的2d数组,因为如果我将其声明为String,则无法再进行比较按整数,如果我将其声明为整数,则无法再通过String进行比较.
I have a 2d array of users, each row has one users data. The data is string or integer respectively down the column so if I have multiple compare methods, such as sort by name or sort by phone number, how can I implement the initial 2d array since if I declare it as a String I can no longer compare by integer, and if i declare it as an integer I can no longer compare via String.
到目前为止,我正在使用基本的2d排序方法.
I am using the basic 2d sort method as of now.
final Integer[][] data1 = userlistcopy;
Arrays.sort(data1, new Comparator<Integer[]>() {
@Override
public int compare(final Integer[] entry1, final Integer[] entry2) {
final Integer time1 = entry1[1];
final Integer time2 = entry2[1];
return time2.compareTo(time1);
}
});
System.out.println("====");
for (final Integer[] s : data1) {
System.out.println(s[0] + " " + s[1]);
}
或
final String[][] data1 = userlistcopy;
Arrays.sort(data1, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String time1 = entry1[3];
final String time2 = entry2[3];
return time2.compareTo(time1);
}
});
System.out.println("====");
for (final String[] s : data1) {
System.out.println(s[0] + " " + s[1]);
}
因此,如果我比较不同的变量类型而又不会使编译器不喜欢我,那么我有什么方法可以转换变量?或者当我只比较数组的整数列时,是否有可能只将整个compareTo方法声明为Integer(甚至包含String)?
So I is there any way to cast the variables if I am comparing a different variable type without making the compiler dislike me? Or is it possible for me to just declare the whole compareTo method an Integer (even with Strings involved) when I am only comparing the integer columns of the array?
谢谢!
编辑,更多详细信息: 我有一个二维数组,即.
EDIT, more detail: I have a 2D array, I.E.
[0] [bob] [3] [joe] [4] [john] [6] [吉米]
[0][bob] [3][joe] [4][john] [6][jimmy]
它存储在String [] [] userlistcopy中; 如果我想使用String进行compareTo,它将可以工作,因为我可以比较userlistcopy [1],即使忽略了整数也可以将其视为字符串. 但是,如果我想通过Integer进行比较,则必须将userlistcopy更改为Integer数组,然后由于它们存在字符串而出现异常,而我忽略了它使String数据槽为空的错误.
It is stored in a String[][] userlistcopy; If I want to compareTo with String, it will work since I can compare userlistcopy[1] and ints can be seen as strings even though they are ignored. However, if I want to compare via Integer, I have to change userlistcopy to an Integer array, and then it freaks out since they're are Strings present, and i ignore the error it nulls out the String data slots.
编辑(显示出来).
好,我知道了!我只是将所有数据传输到一个Object数组中,就像这样做的那样,我声明了它们各自的String/Int类型,然后将其与通过Object进行比较,在实际比较中,我只是将其转换/解析为自己的需要,例如.
Ok, I figured it out! I just transferred all the data into an Object array, as I did so I declared their respective String/Int types, then I compared to via Object, and during the actual comparison I just casted/parsed it to my own needs, such as..
Arrays.sort(data1, new Comparator<Object[]>() {
@Override
public int compare(final Object[] entry1, final Object[] entry2) {
final Integer time1 = Integer.parseInt(entry1[1].toString());
final Integer time2 = Integer.parseInt(entry2[1].toString());
return time2.compareTo(time1);
}
});
或
final Object[][] data1 = datanew;
Arrays.sort(data1, new Comparator<Object[]>() {
@Override
public int compare(final Object[] entry1, final Object[] entry2) {
final String time1 = entry1[2].toString();
final String time2 = entry2[2].toString();
return time2.compareTo(time1);
}
});
不过感谢您的帮助!
推荐答案
整数解决方案:
Arrays.sort(data1, new Comparator<Object[]>() {
@Override
public int compare(final Object[] entry1, final Object[] entry2) {
final Integer time1 = Integer.parseInt(entry1[1].toString());
final Integer time2 = Integer.parseInt(entry2[1].toString());
return time2.compareTo(time1);
}
});
或字符串
final Object[][] data1 = datanew;
Arrays.sort(data1, new Comparator<Object[]>() {
@Override
public int compare(final Object[] entry1, final Object[] entry2) {
final String time1 = entry1[2].toString();
final String time2 = entry2[2].toString();
return time2.compareTo(time1);
}
});
或日期
public int compare(final Object[] entry1, final Object[] entry2) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
time1 = sdf.parse(entry1[3].toString());
time2 = sdf.parse(entry2[3].toString());
return time2.compareTo(time1);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
});
我可以通过将整个2d数组作为一个对象,然后在对象compareTO方法中分别对它们进行比较,来分别比较它们. :)
I can compare them separately by making the whole 2d array an Object, then comparing them separately within the Object compareTO method. :)
这篇关于字符串和整数的CompareTo/Sorting二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!