Skip to content

Instantly share code, notes, and snippets.

@mackatozis
Last active June 15, 2019 18:40
Show Gist options
  • Save mackatozis/8aa81597bc33d885635cb3a9935e5cd5 to your computer and use it in GitHub Desktop.
Save mackatozis/8aa81597bc33d885635cb3a9935e5cd5 to your computer and use it in GitHub Desktop.
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
public class SortMapByValueProperty {
public static void main(String[] args) {
Map<Integer, HtmlTagValues> map = new LinkedHashMap<>();
map.put(1, new HtmlTagValues("Z", "10", "768", "100"));
map.put(2, new HtmlTagValues("A", "10", "768", "100"));
map.put(3, new HtmlTagValues("B", "10", "768", "100"));
map.put(4, new HtmlTagValues("D", "10", "768", "100"));
map.put(5, new HtmlTagValues("C", "10", "768", "100"));
map.forEach((k,v)->System.out.println("id : " + k + " HtmlTagValues : " + v));
Map<Integer, HtmlTagValues> sortedMap = map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.comparing(HtmlTagValues::getTop)))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
sortedMap.forEach((k,v)->System.out.println("id : " + k + " HtmlTagValues : " + v));
}
}
@Data
@AllArgsConstructor
class HtmlTagValues {
private String top;
private String height;
private String width;
private String left;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment