Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Automatic sorting in Java


  • Please log in to reply
5 replies to this topic

#1 ThemePark

ThemePark

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 147 posts

Posted 11 January 2011 - 02:54 PM

I have a class which implements Comparable, and which is compared by an integer in the class. Now I want to make a collection of the classes, and I will update the integer in each object of the class regularly. So I need a collection that will either automatically update whenever I change the integer in an object, or allow me to sort it myself at any point. I've not been able to find a List, Set or Map that seems able to do that though. But is there one, and if not what would be the best solution?
  • 0

#2 GabryelFall

GabryelFall

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 11 January 2011 - 05:24 PM

Post your class please :)
  • 0
~~~~>>>><<<<~~~~
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe

#3 Chunes

Chunes

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 11 January 2011 - 08:50 PM

Any collection or array of objects that implements the Comparable interface can be automatically sorted with Arrays.sort() or Collections.sort().
  • 0

#4 ThemePark

ThemePark

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 147 posts

Posted 12 January 2011 - 06:39 AM

Yeah, but Collections.sort only sorts lists, so I'd have to extract the list from a Set, and then make a new Set with the sorted list, and that seems a bit overkill. And with a Map it wouldn't be possible since only the keys and not the values would be sorted. So isn't there a better/easier way?
  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 12 January 2011 - 09:26 AM

Yes, that's pretty normal as Sets aren't meant to be ordened.

In theory it isn't guaranteed that if you add object A first, and then object B. That the first object in the set will be A and the second B. In practice that's often the case tho..

If you really want a Set to be ordened, then the SortedSet class may suit you better.

A quick override of the add method in the arraylist class may do what you wish. I overrided it, so it will always sort itself after adding an element:

public class Test {
  public static void main(String[] args){
      ArrayList<[B][COLOR="orange"]Integer[/COLOR][/B]> myList = new ArrayList<[COLOR="orange"][B]Integer[/B][/COLOR]>(){
            @Override
            public boolean add([COLOR="orange"][B]Integer [/B][/COLOR]e) {
                boolean result = super.add(e);
                Collections.sort(this);
                return result;
            }
      };

      myList.add(2);
      myList.add(3);
      myList.add(1);
      printList(myList);
      myList.add(7);
      myList.add(1);
      printList(myList);
      myList.add(5);
      myList.add(0);
      printList(myList);
  }

  private static void printList(List<Integer> list){
      System.out.println("\n-------------\n");
      for(Integer value : list){
          System.out.println(value);
      }
  }
}
Note the colored classes must always match eachother.
  • 0

#6 HendraWijayaDjiono

HendraWijayaDjiono

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 22 February 2017 - 09:39 PM

IMHO, @wim DC's answer is very dangerous with performance, especially if the list has many items in it, because it will always re-sorting the list automatically when we add any item to it.


  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download