简体   繁体   中英

How do I sort an ArrayList of objects the way that I want

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?

It would be a lot easier if, instead of String, you would use a custom Object, eg 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.

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);
}

您可以检查Java的流功能,可以根据类的任何变量对对象列表进行排序

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 );

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM