Reputation: 111
I know that this topic can be well covered, but they only use a Treemap which use only one value and in case of same value does not return what I would like.
Here is my object:
public classe myObjectToMap
{
long id;
double price;
String name;
long date;
public static void GetMyObject(long id, double price, String name)
{
myObjectToMap newObject = new myObjectToMap;
newObject.id = id;
newObject.price = price;
newObject.name = name;
newObject.date = new Date().getTime();
PlaceOfMyMap.myMap.put(id, newObject);
}
Here is where my map is located, I put a LinkedHashMap, and I do not which is the best between Hashmap, TreeMap and LinkedHashMap, I did see that TreeMap give a comparator of Value, but I do not arrive to Compare with more than one value.
public class PlaceOfMyMap
{
public static LinkedHashmap<Long, myObjectToMap> myMap = new LinkedHashmap<~>;
}
And finally, here is my main program:
public class MainClass
{
public static void main(String args[]) throws Exception
{
MyObjectToHashmap.GetMyObject(1, 26, "Mat")
MyObjectToHashmap.GetMyObject(4, 25, "Tommy")
MyObjectToHashmap.GetMyObject(16, 24, "Kate")
MyObjectToHashmap.GetMyObject(63, 26, "Mary")
MyObjectToHashmap.GetMyObject(99, 24, "Ronny")
}
}
First: I would like to sort them from the highest price to the lowest with a time priority, which means that I would like to Mat being the first and Kate the second.
Second: I would like to sort them from the Lowest price to the the Highest with a time priority which means that I would like to Kate being the first and Ronny the second.
Any tips to sort them correctly ?
Upvotes: 0
Views: 848
Reputation: 8499
Try the below code. You can reverse the compare()
method to get reverse sorting
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
class MyObject
{
private long id;
private double price;
private String name;
private long date;
public MyObject(long id, double price, String name) {
this.id = id;
this.price = price;
this.name = name;
this.date = new Date().getTime();
}
@Override
public String toString() {
return "MyObject [id=" + id + ", price=" + price + ", name=" + name
+ "]";
}
public double getPrice() {
return price;
}
public long getDate() {
return date;
}
}
public class MapSort {
public static void main(String[] args) {
Map<Long, MyObject> myMap = new LinkedHashMap<Long, MyObject>();
myMap.put(1L, new MyObject(1, 26, "Mat"));
myMap.put(4L, new MyObject(4, 25, "Tommy"));
myMap.put(16L, new MyObject(16, 24, "Kate"));
myMap.put(63L, new MyObject(63, 26, "Mary"));
myMap.put(99L, new MyObject(99, 24, "Ronny"));
System.out.println("Before Sorting");
System.out.println(myMap);
System.out.println("\nAfter Sorting");
System.out.println(sortMap(myMap));
}
private static Map<Long, MyObject> sortMap(
Map<Long, MyObject> unsortedMap) {
List<Entry<Long, MyObject>> list = new LinkedList<Entry<Long, MyObject>>(unsortedMap.entrySet());
Collections.sort(list,
new Comparator<Entry<Long, MyObject>>() {
@Override
public int compare(Entry<Long, MyObject> o1, Entry<Long, MyObject> o2) {
int priceResult = Double.valueOf(o1.getValue().getPrice()).compareTo(Double.valueOf(o2.getValue().getPrice()));
if(priceResult != 0) return priceResult;
return Long.valueOf(o1.getValue().getDate()).compareTo(Long.valueOf(o2.getValue().getDate()));
}
});
Map<Long, MyObject> sortedMap = new LinkedHashMap<Long, MyObject>();
for(Entry<Long, MyObject> item : list){
sortedMap.put(item.getKey(), item.getValue());
}
return sortedMap;
}
}
Output
Before Sorting
{1=MyObject [id=1, price=26.0, name=Mat], 4=MyObject [id=4, price=25.0, name=Tommy], 16=MyObject [id=16, price=24.0, name=Kate], 63=MyObject [id=63, price=26.0, name=Mary], 99=MyObject [id=99, price=24.0, name=Ronny]}
After Sorting
{16=MyObject [id=16, price=24.0, name=Kate], 99=MyObject [id=99, price=24.0, name=Ronny], 4=MyObject [id=4, price=25.0, name=Tommy], 1=MyObject [id=1, price=26.0, name=Mat], 63=MyObject [id=63, price=26.0, name=Mary]}
Upvotes: 1