Getting a collection of Java objects in alphabetical order

I have a question that I really don't know where to start. So I thought I would ask him here.

Basically, I have a drop down list with names. I want these names to be in alphabetical order.

Filling in the drop-down list is as follows:

I query the database and pull out the identifier and name, create an object named "UserList" and set the name and identifier variables so that I will return. Then I add this object to the ArrayList. I do it all over again.

Then I convert this collection to an array and pass it to my JSP page using

session.setAttribute("userList", UserList); 

Then I will close the dropdown menu as shown below.

 <c:forEach items="${userList}" var="c" > `<html-el:option value="${c.id}"><c:out value="${c.name}"/></html-el:option> </c:forEach> 

There is probably a simple answer, but how do I sort these names?

+4
source share
7 answers

Michael, you have to use join and order to get this data from the database, and not retrieve and sort in Java:

  select person.id, person.name from person inner join person_company using(personid) order by person.name; 

And do not try to sort and do it in java (the syntax above may not be ideal, my MySQL is a little rusty).

+3
source

Usually you do this by calling public static Collections.sort(List<T> list) with an ArrayList as a parameter, but take care of implementing the Comparable interface or it will not work (if they are Strings , then it is already implemented):

 Collections.sort(yourList); 

Otherwise, if you have your own class, but you want to sort only some string field inside, you can delegate the compareTo method:

 public class User implements Comparable<User> { public int compareTo(User other) { return userName.compareTo(other.userName); } } 

Finally, if in your case there is not just your own compareTo method, it should return -1 , 0 or 1 if the caller is less than or equal to the passed one.

+8
source

I think that the most ideal solution to this problem is to sort them at the database query level. If the table is well indexed, this will be the fastest and most controversial "best" practice.

If you want to simply sort this list and not start dumping on the database side, use Collections.sort and implement your UserList Comparable<UserList>

Be careful when using case-insensitive string comparisons, or your users will most likely not appreciate the sort results.

+1
source

I would modify the database query to return the rows in the order you need them.

If you are using SQL, the query might look like this.

 SELECT Id, Name FROM Person ORDER BY Name; 

This has some advantages over the execution of this code.

  • It's faster.
  • You do not need to change your code.
  • It works less.
+1
source

You can try and override the compareTo method for UserList so that it compares the elements in alphabetical order according to some string value. Then call the array list sorting method, and it should sort them using compareTo you redo.

0
source

You should use Collections.sort and any of

  • Make your custom class implement Comparable
  • Create a UserListComparator implementing the Comparator<UserList> interface

For more information, see the documentation for the Comparable interface.

0
source

You can sort in the database rather than run many queries with

 select Id, Name from Person where id = '+variable+'; 

You can loop through variables and make a comma-separated list, so you only run one query

 int[] variableList = wherever_you_get_it_from; StringBuffer csv = new StringBuffer(); for (int i = 0; i < variableList.length; i++) { csv.append(variableList[i]); if (i < variableList.length - 1) csv.append(","); } 

then give

 select Id, Name from Person where id in ( +csv.toString()+) order by Name; 

Since you get user IDs from the company table, you can join and get

 select u.usrid, u.name from company c left join user u on c.usrid = u.usrid where c.companyid in (comapnyid1, companyid2, companyid3) order by u.name; 
0
source

Source: https://habr.com/ru/post/1309674/


All Articles