Reputation: 71
I want to sort an ArrayList Objects the way that I want. Thist is my object:
public class PaintRegion extends JPanel {
public ArrayList<Region> m_region = new ArrayList<Region>();
// some codes
}
And its class:
public class Region {
private String m_region;
private Integer m_totalCount;
private double m_normalizedCount;
public String getRegion() {
return m_region;
}
public void setRegion(String region) {
this.m_region=region;
}
public Integer getTotalCount() {
return m_totalCount;
}
public void setTotalCount(Integer totalCount) {
this.m_totalCount = totalCount;
}
public double getNormalizedCount() {
return m_normalizedCount;
}
public void setNormalizedCount(double normalizedCount) {
this.m_normalizedCount = normalizedCount;
}
public boolean Print_Region() {
System.out.println("State::Print_Region(): " +
this.getRegion()+ ", "+
this.getTotalCount()+ ", "+
this.getNormalizedCount());
return true;
}
}
I have 5 region names those are {"Southeast","Southwest","Northeast","West","Midwest"}
and I want to sort this objet by "West","Midwest","Southeast","Southwest","Northeast"
How can I sort?
Upvotes: 2
Views: 95
Reputation: 6302
This is very intelligent way of solving the problem. No need to write all the big logic. Just create an arraylist of sorted region.
Now the great thing about arraylist is elements are indexed and all the element's index is also sorted(0, 1,2,3 etc). Now just use the index itself in sorting the Region.
Hope it's clear.
List<Region> regions = new ArrayList<Region>();
List<String> sortedRegionNames = new ArrayList<>();
sortedRegionNames.addAll(Arrays.asList(new String[]{ "West","Midwest","Southeast","Southwest","Northeast"}));
Comparator<Region> comp = new Comparator<Region>() {
@Override
public int compare(Region r1, Region r2) {
return new Integer(sortedRegionNames.indexOf(r1.getRegion())).compareTo(sortedRegionNames.indexOf(r2.getRegion()));
}
};
Collections.sort(regions, comp );
Upvotes: 0
Reputation: 11992
Because you would like a very specific ordering that may not be suited for other uses of this Region class, my recommendation is to use a Comparator
. Below is a quick draft of what one may look like for your scenario and how to use it.
Comparator<Region> compassComporator = (r1, r2) -> {
//this assumes both objects (and their m_region field) are not null, else do a check for null
if (r1.m_region.equals(r2.m_region)) {
return 0;
}
if ("West".equals(r1.m_region)) {
return 1;
}
if ("West".equals(r2.m_region)) {
return -1;
}
if ("Midwest".equals(r1.m_region)) {
return 1;
}
if ("Midwest".equals(r2.m_region)) {
return -1;
}
if ("Southeast".equals(r1.m_region)) {
return 1;
}
if ("Southeast".equals(r2.m_region)) {
return -1;
}
if ("Southwest".equals(r1.m_region)) {
return 1;
}
if ("Southwest".equals(r2.m_region)) {
return -1;
}
if ("Northeast".equals(r1.m_region)) {
return 1;
}
// if ("Northeast".equals(r2.m_region)) {
return -1;
// }
};
public class PaintRegion extends JPanel {
public ArrayList<Region> m_region = new ArrayList<Region>();
// some codes
m_region.sort(compassComporator);
}
Upvotes: 1
Reputation: 3
You can check stream feature of java there you can sorted your list of object by based on any variable of your class
Upvotes: 0
Reputation: 298818
It would be a lot easier if, instead of String, you would use a custom Object, e.g. a class called Region (properties: name and sort index). Then you could implement Comparable and sort it with Collections.sort(). Or use a sorted collection type like TreeSet to begin with.
Upvotes: 2