Java sorting List

java, list

Solution

Have your class implement `Comparable` and provide said sort ordering in `compareTo` method. And to sort, simply use `Collections#sort`, although be aware that it's an inline sort.

Problem

I got a List in java. I get values from a SQL query. ``` public void ReloadPages() throws Exception { try (Connection conn = Framework.GetDatabaseManager().GetBone().getConnection()) { try (ResultSet set = conn.createStatement().executeQuery("SELECT * FROM habbo_shop_pages")) { while (set.next()) { int Id = set.getInt(1); Pages.put(Id, new CatalogPage(set)); } } } System.out.println("Loaded " + Pages.size() + " Catalog Page(s)."); } ``` Then I store it all. In another function, I want to retrieve certain pages from a parentid. ``` public LinkedList<CatalogPage> getSubPages(int parentId) { LinkedList<CatalogPage> pages = new LinkedList<>(); for (CatalogPage page : this.Pages.values()) { if (page.getParentId() != parentId) { continue; } pages.add(page); } return pages; } ``` How do I order the list? Now id 4 is above in the shop and 1 at the bottom, but I want it ordered by id. ORDER BY in query doesn't work.

Original source