如何像 SQL 中的 ORDER BY 子句一样操作数组?

发布于 2024-08-21 10:27:33 字数 862 浏览 7 评论 0原文

我的数据已放入数组中,但不幸的是不是按照我想要的顺序...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-08-28 10:27:33

您需要将 Comparator 传递给 Arrays.sort 方法。我已经 5 年没有学过 Java了,但是应该很容易做到。也许有人可以清理我即将写的这个例子,因为我很确定我会出错。

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"    }
};

Arrays.sort(databaseToArray, new Comparator<String[]>() {
    public int compare(String[] a, String[] b) {
        return a[3].compareTo(b[3]);
    }
});

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.

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"    }
};

Arrays.sort(databaseToArray, new Comparator<String[]>() {
    public int compare(String[] a, String[] b) {
        return a[3].compareTo(b[3]);
    }
});
请别遗忘我 2024-08-28 10:27:33

我在处理网站上找到了排序方法,但这仅适用于一维数组...

http: //processing.org/reference/sort_.html

   float[] a = { 3, 5, 2, 4, 1 };
   a = sort(a);
   println(a);
// Prints 1, 2, 3, 4, 5

I found the sort method on the processing website, but this is only for One-Dimensional Arrays...

http://processing.org/reference/sort_.html

   float[] a = { 3, 5, 2, 4, 1 };
   a = sort(a);
   println(a);
// Prints 1, 2, 3, 4, 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文