How to sort a list of structures using a structure element in java

I have a list of structures that I would like to sort according to a specific element of the structure:

private class myStruct { public Boolean GUI; public float CallTime; public String ReqID; public String ReqGUID; public String Stereotype; public String StereotypeGUID; } private List<myStruct> DataList = new ArrayList<myStruct>(); 

How can I sort a DataList using the "ReqID" element without hard coding it? Is it possible to use Arrays.sort() ?

+6
source share
8 answers

You must use Comparator .

  class YourComparator implements Comparator<myStruct>{ public int compare(myStruct s1, myStruct s2){ //here comes the comparison logic } } 

And then use this form of the sort() method:

  Arrays.sort(T[] arrayToSort, Comparator<T> yourComparator); 

It's not very clear if you use a collection or an array as a data structure.

If you are using a List, use Collections.sort() .

+2
source

For custom sorting, you can implement the Comparable interface.

Using this interface, you create a compareTo() method that returns a negative number, 0 or a positive number. Based on the Collections.sort() return code, you can determine whether an item should be before or after another item.

A good example of how to use it can be found in this answer: the Java class implements comparable

+1
source

use a comparator interface like this

 public static void main(String[] args) { List<myStruct> DataList = new ArrayList<myStruct>(); //ADD Objects to DataList here Collections.sort(DataList, new Comparator() { public int compare(Object o1, Object o2) { myStruct p1 = (myStruct) o1; myStruct p2 = (myStruct) o2; int ret = -1; //business logic here if (Integer.parseInt(p1.ReqGUID) == Integer.parseInt(p2.ReqGUID)) { ret = 0; } else if (Integer.parseInt(p1.ReqGUID) > Integer.parseInt(p2.ReqGUID)) { ret = 1; } else if (Integer.parseInt(p1.ReqGUID) < Integer.parseInt(p2.ReqGUID)) { ret = -1; }//end business logic return ret; } }); } 

Inside the Collections.sort () method, I implement the Comparator interface and override the compare () method. This actually sorts your list based on the business logic implemented inside the compare () method;

+1
source

The structure of your class looks strange to me. You have public fields inside a private class. Ideally, your fields should be privately allocated, and you can have getters and setters to access them.

As for your problem, you can take a look at two important interfaces that are defined for this kind of work: - http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html and http : //docs.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html ..

You use Comparator if you want to have several ways to compare an instance of a class. You can simply create a class that implements the Comparator interface and pass an instance of this class to the Collections.sort () method to use this Comparator for sorting. In this case, the compare () method is used to perform the comparison.

Alternatively, you can only link one way to compare an instance of a class by creating a Comparable interface for that class. In this case, you need to override the compareTo () method.

Here is an example code using a comparator: -

 public class MyComparator implements Comparator<Box> { @Override public int compare(Box box0, Box box1) { int w0 = box0.getWeight(); int w1 = box1.getWeight(); return (w0 > w1? -1 : (w0 == w1) ? 0 : 1); } } public class Box { private int weight; public Box() { } public Box(int weight) { this.weight = weight; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } } 

And your main

 public class Main { public static void main(String args[]) { List<Box> boxList = new ArrayList<Box>(); Collections.sort(boxList, new MyComparator()); } } 

Hope this helps.

+1
source

You can define your Comparator and use Collections.sort () by passing this comparator to. Thus, you can define different comparators for searching using different fields.

Alternatively, your structure can implement the Comparable interface and Collections.sort() can sort using this. This is called using the natural sort order, as it implicitly refers to your class.

Here's a Java tutorial on sorting and organizing .

0
source

Use java.util.Collections.sort() with an instance of Comparator . See JavaDocs.

0
source

Use Arrays.sort(T[] a, Comparator<? super T> c)

or Collections.sort(List a, Comparator c)

0
source

Simplest solution

Just implement the java.lang.Comparable interface in your class, for example:

 class MyStruct implements Comparable<MyStruct>{ public Boolean GUI; public float CallTime; public String ReqID; public String ReqGUID; public String Stereotype; public String StereotypeGUID; @Override public int compareTo(MyStruct other) { return ReqID.compareTo(other.ReqID); /* also you can use ReqID.compareToIgnoreCase(other.ReqID); */ } @Override public String toString() { return "(" + ReqID + ")"; } } 

Override print only toString () method.

Also, keep in mind that the String compareTo () method is sorted using lexicographic . If you need a numeric identifier, it is better to use int or another numeric type. Below is the full code for sorting using Arrays.sort (), as well as Collections.sort () - choose what suits you :)

 public class MyStructSort { private final static String[] STRUCT_IDS = {"C", "D", "A", "Aa", "B", "Z", "Aaa" }; private static List<MyStruct> createList() { List<MyStruct> structList = new ArrayList<MyStruct>(); for (String id: STRUCT_IDS) { MyStruct struct = new MyStruct(); struct.ReqID = id; structList.add(struct); } return structList; } public static void main(String[] args) { List<MyStruct> dataList = createList(); /* Sort using Lists (Collections) */ Collections.sort(dataList); System.out.println("List sort:\t" + dataList); /* Sort using arrays */ MyStruct[] dataArray = dataList.toArray(new MyStruct[dataList.size()]); Arrays.sort(dataArray); // print sorted array System.out.print("Array sort:\t"); for (MyStruct struct: dataArray) { System.out.print(struct+" "); } } } 

This is just a demo code, so some null checks, getters and setters will be required to improve it.

0
source

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


All Articles