participate


Collections: Lists, Sets, and Maps - Iterate over an unsorted list - in sorted order
<<   Back to Forum  |   Give us Feedback
This topic has 3 replies on 1 page.
appoooh
Posts:28
Registered: 5/1/04
Iterate over an unsorted list - in sorted order   
Apr 11, 2005 4:43 AM

 
Dear all,

What's the preferred way to get an iterator that iterates over all elements
of an unsorted list, in sorted order? My approach was to create a copy of
the list, sort it and then return the sorted list's iterator.

This seems crude ... is it?

Cheers,
David
public Iterator getSortedIterator() {
        List l = new ArrayList();
        l.addAll(myCollection.values());
        Collections.sort(l, new FooModelComparator());
        return l.iterator();
    }
 
private static class FooModelComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Foo s1 = (Foo) o1;
            Foo s2 = (Foo) o2;
            return s1.getId().compareTo(s2.getId());
        }
    }
 
nasch_
Posts:6,747
Registered: 1/25/04
Re: Iterate over an unsorted list - in sorted order   
Apr 11, 2005 8:16 AM (reply 1 of 3)  (In reply to original post )

 
If getSortedIterator is a method of a List class, then I think that's not a good idea. If it's in some other class that's using a List, go for it.
 
Malvolio
Posts:314
Registered: 11/20/04
Re: Iterate over an unsorted list - in sorted order   
Apr 11, 2005 9:48 AM (reply 2 of 3)  (In reply to original post )

 
Dear all,

What's the preferred way to get an iterator that
iterates over all elements
of an unsorted list, in sorted order? My approach
was to create a copy of
the list, sort it and then return the sorted list's
iterator.

This seems crude ... is it?

No, I think it's about the best you could do algorithmically. Your code could be improved though:
public Iterator getSortedIterator() {
    List l = new ArrayList(myCollection.values());
    Collections.sort(l, new Comparator() {
            public int compare(Object o1, Object o2) {
                Foo s1 = (Foo) o1;
                Foo s2 = (Foo) o2;
                return s1.getId().compareTo(s2.getId());
            }});
 
    return l.iterator();
}
 
Chris@WSU
Posts:262
Registered: 3/30/05
Re: Iterate over an unsorted list - in sorted order   
Apr 11, 2005 10:38 AM (reply 3 of 3)  (In reply to original post )

 
can do it all in one place, write a iterator class
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
 
/** SortedIterator
 * @author CNLohfin3109
 * Created on Apr 11, 2005
 */
public class SortedIterator implements Iterator {
       private Object o[];
       int i;
       public SortedIterator(Collection c){
           	o = c.toArray();
          	Arrays.sort(o);
          	i=0;
       }
 
       public Object next(){
           return o[i++];
       }
 
       public boolean hasNext(){
          return ( i < o.length );
       }
 
       public void remove(){
           throw new UnsupportedOperationException();
       }
}


then just have your method return new SortedIterator(myCollection);
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums