Jump to content

Automatic sorting in Java

- - - - -

  • Please log in to reply
4 replies to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
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?

#2
GabryelFall

GabryelFall

    Learning Programmer

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

#3
Chunes

Chunes

    Newbie

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

#4
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
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?

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users