简体   繁体   中英

Sorting an arraylist

I am looking to sort my array by the name of a company, printing only the companies that are open before a specific time...... before printing

public void printCompanies(int time)
{
    Iterator <Company> it = CompanyList.iterator();
    while( it.hasNext()) {
        Company a = it.next();
        if(a.getTime() == (time)) {
            Collections.sort(CompanyList.getName());
        }
    }
}

I am getting the error, cant find method getName(), this method is featured in another class. That is not inherited etc.

Am i on the right lines?/How would I go about fixing this error

If I use

   Collections.sort(Company.getName());

then I get the error "non-static method getName() cannot be referenced from a static context?

As far as I understood, CompanyList is a list while getName() is a method of an individual company. That's where the error comes from.

As to make the code work as you want it, you need to understand how Collections.sort ( JavaDoc ) works. The easiest way to make it sort the companies by name is to modify the class Company to implement the interface Comparable<Company> ( JavaDoc ) and implement the compareTo method so it compares the names of two companies. Assuming the names are Strings it'll probably be no more complex than

int compareTo(Company other) {
    return this.getName().compareTo(other.getName());
}

Alternatively, you could use Comparators ( JavaDoc ) which are slightly more complex but more flexible, if you need to sort according to different criteria for instance.

The short answer is that, no, you are not on the right path ;-)

The longer version of that answer is that you can't call Collections.sort on a member of the list, you need to call it on the list.

By default, Collections.sort will use the natural sort of the members, if you want to change that you need to pass in your own comparator.

Once you have sorted the list, then you can iterate over it to filter it and perform some processing on the result.

So, you want to first sort the list, and then filter it, so something like:

public void printCompanies(int time)
{
  Collections.sort(companyList, 
                   new Comparator() {
                     public int compare(Object o1, Object o2) {
                       if (o1 == o2)
                         return 0;
                       if (o1 == null)
                         return -1;
                       if (o2 == null)
                         return 1;
                       Company c1 = (Company) o1;
                       Company c2 = (Company) o2;
                       return c1.getName().compareTo(c2.getName());
                     }
                   });
  Iterator <Company> it = companyList.iterator();
  while(it.hasNext()) {
    Company a = it.next();
    if(a.getTime() <= (time)) {
      // Do something
    }
  }
}

Like others have said, you'll want to sort once before iterating. What you need to do in order to sort on name, though, is either change the Company class to implement the Comparable<Company> interface (the preferred solution) or add an inner class where you're iterating that implements Comparator<Company> . Either way you go, you'll be calling something like companyA.getName().compareTo(companyB.getName()) in the method that the interface will force you to implement.

If you take the first route, you can just call Collections.sort(list); then iterate over the list. The second route will force you to call Collections.sort(list, new MyCustomComparator());

BTW, if your CompanyList is iterable, just use for (Company company : companyList) {...} instead of creating an iterator after you call one of these sorting methods.

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