Java 实现多列数组排序
我正在尝试对数组中的数据进行排序,如下所示:Java 实现多列数组排序,java,arrays,sorting,Java,Arrays,Sorting,我正在尝试对数组中的数据进行排序,如下所示: (String, int, String, double) 或 我希望数组排序的描述由“整型”值决定。例如: David, 25, Da, 123.54 Sean, 27, Pa, 514.21 Luke, 32, Ma, 221.54 这些值从文件中读取,组织在构造函数中,然后发送回驱动程序 以下是我目前拥有的,我不确定它会有多大用处: public class NamesAges{ public String[] dis
(String, int, String, double)
或
我希望数组排序的描述由“整型”值决定。例如:
David, 25, Da, 123.54
Sean, 27, Pa, 514.21
Luke, 32, Ma, 221.54
这些值从文件中读取,组织在构造函数中,然后发送回驱动程序
以下是我目前拥有的,我不确定它会有多大用处:
public class NamesAges{
public String[] display() throws IOException {
FileReader fileReader = new FileReader("elements.csv");
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
System.out.println(line);
}
bufferedReader.close();
String[] sl = (String[]) lines.toArray(new String[0]);
return sl;
}
}
公共类名称{
公共字符串[]display()引发IOException{
FileReader FileReader=newFileReader(“elements.csv”);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
列表行=新的ArrayList();
字符串行=null;
而((line=bufferedReader.readLine())!=null)
{
行。添加(行);
系统输出打印项次(行);
}
bufferedReader.close();
字符串[]sl=(字符串[])行。toArray(新字符串[0]);
返回sl;
}
}
有什么建议吗 我建议将每一行封装到一个
对象中
,并编写一个比较器
,根据需要对它们进行排序。Java是一种面向对象的语言;不要再考虑原语和字符串。我建议将每一行封装到对象中,并编写比较器来根据需要对它们进行排序。Java是一种面向对象的语言;不要再考虑原语和字符串。最简单的方法可能是创建一个封装这四个值的类,使该类实现Compariable
,并通过integer定义其Comparieto()
方法。最简单的方法可能是创建一个封装这四个值的类,使类实现Comparable
并通过整数定义其compareTo()
方法。但是String
s不是原语。它们是您使用它们的方式。True。不过,你的措辞有点让人困惑。但是String
s不是原语。它们是你使用它们的方式。是的。不过,您的措辞有点混乱,“我希望数组排序的描述由“整”值决定。”好的。如果正好有一列具有整数值,那么这是如何进行“多列”排序的?”我希望数组排序的描述由“整数值”决定。好的。如果正好有一列具有整数值,那么这是如何进行“多列”排序的?
public class NamesAges{
public String[] display() throws IOException {
FileReader fileReader = new FileReader("elements.csv");
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
System.out.println(line);
}
bufferedReader.close();
String[] sl = (String[]) lines.toArray(new String[0]);
return sl;
}
}