如何像 SQL 中的 ORDER BY 子句一样操作数组?
我的数据已放入数组中,但不幸的是不是按照我想要的顺序...
String[][] databaseToArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"John", "Nick", "likes", "2", "yes" },
{"Drew", "MTV", "dislikes", "4", "no" },
{"Fred", "CNN", "okay", "3", "no" },
{"Beth", "Fox", "valid", "1", "yes" }
};
我如何操作这个数组,以便当我循环它时,顺序是按 amount ,类似于 SELECT * FROM “databaseToArray” ORDER BY “Amount” 又名
String[][] reorganizedArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"Beth", "Fox", "valid", "1", "yes" },
{"John", "Nick", "likes", "2", "yes" },
{"Fred", "CNN", "okay", "3", "no" },
{"Drew", "MTV", "dislikes", "4", "no" }
};
My data has been placed into an array, but unfortunately not in the order I want it in...
String[][] databaseToArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"John", "Nick", "likes", "2", "yes" },
{"Drew", "MTV", "dislikes", "4", "no" },
{"Fred", "CNN", "okay", "3", "no" },
{"Beth", "Fox", "valid", "1", "yes" }
};
How do I manipulate this array so that when I loop through it the order is by the amount , similar to SELECT * FROM "databaseToArray" ORDER BY "Amount"
aka
String[][] reorganizedArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"Beth", "Fox", "valid", "1", "yes" },
{"John", "Nick", "likes", "2", "yes" },
{"Fred", "CNN", "okay", "3", "no" },
{"Drew", "MTV", "dislikes", "4", "no" }
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 Comparator 传递给 Arrays.sort 方法。我已经 5 年没有学过 Java了,但是应该很容易做到。也许有人可以清理我即将写的这个例子,因为我很确定我会出错。
You'll want to pass a Comparator to the Arrays.sort method. I haven't done Java in 5 years, but it should be easily doable. Maybe someone can clean up this example I'm about to write, since I'm pretty certain I'll get something wrong.
我在处理网站上找到了排序方法,但这仅适用于一维数组...
http: //processing.org/reference/sort_.html
I found the sort method on the processing website, but this is only for One-Dimensional Arrays...
http://processing.org/reference/sort_.html