How to sort Array Of DashboardObjectAIR object If suppose any array object in contains in arrray 0 or 1 location "UnOk" then it should show top.
For below tempObj
List the airObj
should in location 0 in list because it contains "UnOk".
DashboardObjectAIR[] airObj = new DashboardObjectAIR[2];
airObj[0] = new DashboardObjectAIR("UnOk");
airObj[1] = new DashboardObjectAIR("Ok");
airObj1[0] = new DashboardObjectAIR( "Ok");
airObj1[1] = new DashboardObjectAIR("Ok");
List<DashboardObjectAIR[]> tempObj = new ArrayList<DashboardObjectAIR[]>();
tempObj.add(airObj);
tempObj.add(airObj1);
How can i sort this scnarion:
Anyone can help me please ?
Try below
assuming the array will have minimum 2 elements, if not have to add boundary checks
DashboardObjectAIR[] airObj = new DashboardObjectAIR[2];
airObj[0] = new DashboardObjectAIR("UnOk");
airObj[1] = new DashboardObjectAIR("Ok");
DashboardObjectAIR[] airObj1 = new DashboardObjectAIR[2];
airObj1[0] = new DashboardObjectAIR("Ok");
airObj1[1] = new DashboardObjectAIR("Ok");
DashboardObjectAIR[] airObj3 = new DashboardObjectAIR[2];
airObj3[0] = new DashboardObjectAIR("Ok");
airObj3[1] = new DashboardObjectAIR("Ok");
DashboardObjectAIR[] airObj4 = new DashboardObjectAIR[2];
airObj4[0] = new DashboardObjectAIR("Ok");
airObj4[1] = new DashboardObjectAIR("UnOk");
List<DashboardObjectAIR[]> listOfArray = new ArrayList<DashboardObjectAIR[]>();
listOfArray.add(airObj);
listOfArray.add(airObj1);
listOfArray.add(airObj3);
listOfArray.add(airObj4);
Comparator<DashboardObjectAIR[]> c = (a1, a2) -> {
int comp = a2[0].getOk().compareTo(a1[0].getOk()); // note a2 - a1 to have Unok before Ok
if (comp == 0)
return a2[1].getOk().compareTo(a1[1].getOk());
else
return comp;
};
List<DashboardObjectAIR[]> sorted = listOfArray.stream().sorted(c).collect(Collectors.toList());
sorted.forEach(arr -> System.out.println(Arrays.toString(arr)));
output
[DashboardObjectAIR [ok=UnOk], DashboardObjectAIR [ok=Ok]]
[DashboardObjectAIR [ok=Ok], DashboardObjectAIR [ok=UnOk]]
[DashboardObjectAIR [ok=Ok], DashboardObjectAIR [ok=Ok]]
[DashboardObjectAIR [ok=Ok], DashboardObjectAIR [ok=Ok]]
Edit-1
Array of unknown size [Thanks @Andreas]
Comparator<DashboardObjectAIR[]> c1 = (a1, a2) -> {
return Arrays.stream(a2).map(DashboardObjectAIR::getOk).sorted().collect(Collectors.joining())
.compareTo(Arrays.stream(a1).map(DashboardObjectAIR::getOk).sorted().collect(Collectors.joining()));
};
For the sake of simplicity, I will assume that DashboardObjectAIR
has a boolean isOk()
method, for testing whether the object is "UnOk" or "Ok".
The goal is to sort a List<DashboardObjectAIR[]>
such that arrays that contain an "UnOk" DashboardObjectAIR
object will sort first.
We can use the fact that Boolean.compareTo()
sorts false
before true
, so if we can get a false
value for a DashboardObjectAIR[]
that has an "UnOk" object, then it'll be easy.
Using Java 8 Streams, we can easily do that this way:
tempObj.sort(Comparator.comparing(a -> Arrays.stream(a).allMatch(DashboardObjectAIR::isOk)));
Here, we sort the list by examining each element of the list. The element is assigned to a
of type DashboardObjectAIR[]
and we then check if all objects of the array are "Ok". If so, we return true
(sort last), otherwise we return false
(sort first).
This will work for lists and arrays of any size.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.