Sorting of User Defined Vector - java

In my java program i have created a Vector of user defined type
Vector<PolledData> poll=null;. The Vector is of type of instance of class PolledData.
I need to sort the Vector poll, I tried Collections.sort(poll);,but it didn't work.
Is there a any way to sort the vector poll?

The Solution is:
Collections.sort(poll, new Comparator<PolledData>() {
#Override
public int compare(final PolledData object1, final PolledData object2) {
return object1.getName().compareTo(object2.getName());
}
} );
Now the elements in my Vector poll is sorted.

You need to implement Comparable / Compactor for pojo which are being stored in vector.

Related

How to sort an ArrayList using a Map key as list object and value as the order?

I need to keep the order of my list of objects as it can be changed and reordered anytime in my webapp.
In my model I have a array list of objects example
objectList = [object1, object2, object3, object4]
and I save the order of each object belonging to this instance to a Map example:
order
{
"object1":4,
"object2":2,
"object3":1,
"object4":3
}
Therefore i want to sort my array according to its value in the map retrieved by the objects Id as the key:
Expected result:
[object3, object2, object4, object1]
How would I go about doing this?
Assuming your objects have a getter for the id getId(), then you can make use of the Comparator.comparing() helper method, which creates a comparator for the provided lambda:
objectList.sort(Comparator.comparing(o -> yourMap.get(o.getId()));
This effectively the same as writing your own Comparator or writing your own compare method.
Something that may look overkilling but it could be more natural to define a new class
class IndexedElement implments Comparable<Integer> {
private final int index;
private final YourObject element;
public IndexedElement(int index, YourObject element) {
this.index = index;
this.element = element;
}
#Override
public int compareTo(Integer o) {
return this.getIndex().compareTo(o.getIndex()) ;
}
}
storing objects like that in a TreeSet<IndexedElement> you can automatically achieve the sorting and retrieving (TreeSet.ceiling(new IndexedElement(yourIndex, null)), just remember to Override the equals/hashCode in IndexedElement)

Sorting an ArrayList using content in the ArrayList in Java

I have a ArrayList "R" which is a collection of objects for the class WordRanking.
WordRanking class has 2 variables in it.
1.int Frequency
2.String word
Im trying to sort ArrayList R using Frequency
Best way would be to create a custom comparator to define relation between the WordRanking object then using the Collections.sort(list, comparator); call.
It should look something like this
public class SomeComarator implements Comparator<WordRanking > {
#Override
public int compare(ActiveAlarm x, ActiveAlarm y) {
// return 1 if x>y , -1 if y>x , and 0 if equals
}
}
SomeComarator wordComperator = new SomeComarator ();
Collections.sort(R, wordComperator );`
On a side note - please don't variable names like 'R'.
And, you know, google.

Java order a collection

I have this map:
Map<Integer,List<EventiPerGiorno>> mapEventi=new HashMap<Integer,List<EventiPerGiorno>>();
where EventiPerGiorno is a Comparable object.
How can I get a sorted list from the map?
I tried with
Collection<List<EventiPerGiorno>> collection=mapEventi.values()
Comparable.sort(collection);
But Comparable.sort() doesn't like the list as comparable. Is there a ComparableList?
EDIT
this is the comparable method...
public class EventiPerGiorno implements Comparable<EventiPerGiorno>{
#Override
public int compareTo(EventiPerGiorno o) {
return this.getPrimoSpettacolo().compareTo(o.getPrimoSpettacolo());
}
}
Java Collections don't have any order associated with them. You could turn the Collection into a List first and then sort it.
Collection<List<EventiPerGiorno>> collection = mapEventi.values()
YourComparableList<List<EventiPerGiorno>> list = new YourComparableList(collection);
Collections.sort(list);
For this, you will need to create some sort of List that implements Comparable. See How do I correctly implement Comparable for List in this instance? for an example.
Note that this is sorting the objects of type List<EventiPerGiorno>, not the objects of type EventiPerGiorno. If you are interested in sorting the latter, you might want this instead:
ArrayList<EventiPerGiorno> bigList = new ArrayList<EventiPerGiorno>();
for (List<EventiPerGiorno> list : mapEventi.values()) {
bigList.addAll(list);
}
Collections.sort(bigList);
You are attempting to sort a List of Lists. List doesn't implement Comparable. You'll have to create your own Comparator instance.
Map<String, List<EventiPerGiorno>> map = new HashMap<String, List<EventiPerGiorno>>();
List<List<EventiPerGiorno>> lists = new ArrayList(map.values());
Collections.sort(lists, new Comparator<List<EventiPerGiorno>>() {
#Override
public int compare(List<EventiPerGiorno> o1, List<EventiPerGiorno> o2) {
// ??? This is up to you.
return 0;
}
});
This will sort every list in your map:
for (List<EventiPerGiorno> list : mapEventi.values()) {
Collections.sort(list);
}
Or if you perhaps want to retrieve a single sorted list without modifying the lists in the map:
int someKey = ...;
List<EventiPerGiorno> list = new ArrayList<>(mapEventi.get(someKey));
Collections.sort(list);
return list;
You would need to extend List and make it implement Comparable. There is no default natural ordering that can be used to compare multiple Lists.
The collections framework wouldnt know if you wanted to sort the list by number of items, number of duplicates, or values within the list.
Then to sort you use:
http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List%29

Sorting a list consisting of integer pairs

As the title suggests, I have a list consisting of pairs of integers (int ai and int bi). I want to sort this list based on only upon int a, while preserving the pairwise relationship. I was wondering if there was an efficient way to do this with some of the standard libraries Java has. Thanks in advance!
Edit:
My exact implementation is an ArrayList<ArrayList<Integer>> in which each ArrayList<Integer> has exactly two integers (ai and bi). Sorry for any confusion.
Use the Collections sort() or Arrays sort() method which takes a Comparator and use a custom comparator which only inspects the first integer in the pair.
Something like this (roughly, depending on your exact types):
Collections.sort(myList, new Comparator<IntegerPair>() {
#Override public int compare(IntegerPair x, IntegerPair y) {
return x.first - y.first;
}
});
Since the sorting algorithms are stable (per the Javadocs) your list will be sorted per your description.
Implement http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html for your integer pairs and use sort() from http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html
I would recommend to create a class that represents integer pair. This class should implement Comparable. The use sort() to sort it.
It may be a little safer to use the already-defined Integer compare:
Collections.sort(myList, new Comparator<IntegerPair>() {
#Override public int compare(IntegerPair x, IntegerPair y) {
return Integer.compare(x.first, y.first);
}
});

Sorting Arrays alphabeticly

How can I sort an array in Java/Android alphabetically?
After that I want to provide the ordered array to a ListView.
Arrays.sort() should do the trick
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html
it can be done just using
Arrays.sort(myarray);
For more complex sorting see Comparator
Example:
Arrays.sort(some_array, new Comparator<SomeObject>() {
#Override
public int compare(SomeObject entry1, SomeObject entry2) {
return entry1.getSomeData().compareTo(entry2.getSomeData());
}
});
Objects that implement the Comparable interface (like Strings) have a natural ordering (as defined by its compareTo method) so you can just use Arrays.sort():
Arrays.sort(yourArrayOfComparables);
It will directly sort the array and will not make a copy.
If you want a more specific sort that isn't considered "natural" you can create a Comparator for that object:
Comparator<MyObject> comp = new Comparator<MyObject>() {
public int compare(MyObject obj1, MyObject obj2) {
//Code here
}
}
Arrays.sort(yourArrayOfObjects, comp);
The rules for Comparators and Comparables (where the first object is the object itself) is: return a negative if the first object is less than the second, positive for the other way round and 0 if they're equal.

Categories