I know that this question might have been asked before, but I was not able to find a fit answer. So say I have this array:
String[][] theArray = {
{"james", "30.0"},
{"joyce", "35.0"},
{"frank", "3.0"},
{"zach", "34.0"}};
Is there a way to descendingly sort this array by the second element of each sub-element. So I would get something like this.
theArray = {
{"joyce", "35.0"},
{"zach", "34.0"},
{"james", "30.0"},
{"frank", "3.0"}};
Use Arrays.sort(arr, comparator) with a custom comparator:
Arrays.sort(theArray, new Comparator<String[]>(){
#Override
public int compare(final String[] first, final String[] second){
// here you should usually check that first and second
// a) are not null and b) have at least two items
// updated after comments: comparing Double, not Strings
// makes more sense, thanks Bart Kiers
return Double.valueOf(second[1]).compareTo(
Double.valueOf(first[1])
);
}
});
System.out.println(Arrays.deepToString(theArray));
Output:
[[joyce, 35.0], [zach, 34.0], [james, 30.0], [frank, 23.0]]
Beware:
you will be sorting the array you passed in, Arrays.sort() will not return a new array (in fact it returns void). If you want a sorted copy, do this:
String[][] theCopy = Arrays.copyOf(theArray, theArray.length);
And perform the sorting on theCopy, not theArray.
You must use the Arrays.sort() method. This method takes a Comparator as argument. The sort method delegates to the comparator to determine if one element of the array must be considered bigger, smaller or equal to another element. Since every element of the outer array is an array, the comparator will have to compare arrays (of Strings).
The arrays must be compared based on the value of their second element. This second element is a String which in fact represents a double number. So you'll have to transorm the strings into numbers, else the order will be lexicographical (20 come before 3) rather than numerical.
The comparator could thus look like this :
public class StrinArrayComparator implements Comparator<String[]> {
#Override
public int compare(String[] array1, String[] array2) {
// get the second element of each array, andtransform it into a Double
Double d1 = Double.valueOf(array1.[1]);
Double d2 = Double.valueOf(array2.[1]);
// since you want a descending order, you need to negate the
// comparison of the double
return -d1.compareTo(d2);
// or : return d2.compareTo(d1);
}
}
If you want to move away from arrays, here's a variation that uses List<Record> and a RecordComparator that implements Comparator<Record>.
Console:
joyce 35.0
zach 34.0
james 30.0
frank 23.0
Code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/** #see http://stackoverflow.com/questions/5064027 */
public class ComparatorTest {
public static void main(String[] args) {
List<Record> list = new ArrayList<Record>(Arrays.asList(
new Record("james", "30.0"),
new Record("joyce", "35.0"),
new Record("frank", "23.0"),
new Record("zach", "34.0")));
print(list, Sort.DESCENDING, Field.D);
}
private static void print(List<Record> list, Sort s, Field f) {
RecordComparator rc = new RecordComparator(s, f);
Collections.sort(list, rc);
for (Record r : list) {
System.out.println(r);
}
}
}
class Record {
private String s;
private Double d;
public Record(String name, String number) {
this.s = name;
this.d = Double.valueOf(number);
}
#Override
public String toString() {
return s + " " + d;
}
public int compareTo(Field field, Record record) {
switch (field) {
case S: return this.s.compareTo(record.s);
case D: return this.d.compareTo(record.d);
default: throw new IllegalArgumentException(
"Unable to sort Records by " + field.getType());
}
}
}
enum Sort { ASCENDING, DESCENDING; }
enum Field {
S(String.class), D(Double.class);
private Class type;
Field(Class<? extends Comparable> type) {
this.type = type;
}
public Class getType() {
return type;
}
}
class RecordComparator implements Comparator<Record> {
private Field field;
private Sort sort;
public RecordComparator(Sort sort, Field field) {
this.sort = sort;
this.field = field;
}
#Override
public final int compare(Record a, Record b) {
int result = a.compareTo(field, b);
if (sort == Sort.ASCENDING) return result;
else return -result;
}
}
You seem to be living in object denial. Those inner arrays look a lot like information about a Person (with the name and some value, maybe a score).
What you'd want to do is to write a custom class to hold that information:
public class Person {
private final String name;
private final double score;
public Person(final String name, final double score) {
this.name=name;
this.score=score;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
Then, when you want to sort them, you simply implement a Comparator<Person> that specifies how you want them sorted:
public PersonScoreComparator implements Comparator<Person> {
public int compare(Person p1, Person p2) {
return Double.compare(p1.getScore(), p2.getScore());
}
}
Alternatively, you could have the Person class itself implement Comparable<Person> by adding this method:
public int compareTo(Person other) {
return Double.compare(getScore(), other.getScore());
}
-Create list out of this array using Arrays.toList()
-Design comparator using java.lang.comparator and write logic for sorting every even elements
There are several sort methods in java.util.Arrays. Two of them take custom Comparators. Simply provide a comparator comparing the second element of the inner arrays.
public static void main(String[] args)
{
String Name[][]={{"prakash","kumar"},{"raj","kappor"},{"vinod","bhart"}};
String str[]=new String[2];
for(int j=0; j<Name.length;j++)
{
for (int i=0 ; i<2; i++)
{
str[i]=Name[j][i];
}
for(int i=0;i<str.length;i++)
{
for(int k=i+1;k<str.length;k++)
{
if(str[i].compareTo(str[k])>0)
{
String temp= str[i];
str[i]=str[k];
str[k]=temp;
}
}
System.out.print(str[i]+ " ");
}
System.out.println();
}
}
}
/**
*
* #param array - 2D array required to be arranged by certain column
* #param columnIndex - starts from 0; this will be the main comparator
* #param hasHeaders - true/false; true - ignore the first row. False -
* first row it's also compared and arranged
* #return - the new arranged array
*/
private String[][] arrangeArray(String[][] array, int columnIndex, boolean hasHeaders) {
int headersExists = 0;
if (hasHeaders) {
headersExists = 1;
}
for (int i = headersExists; i < array.length; i++) {
for (int j = headersExists; j < array.length; j++) {
if (array[i][columnIndex].compareTo(array[j][columnIndex]) < 0){
String[] temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
Related
I have a method that sorts a List by different criteria and returns the name (an instance variable) of the one with maximum value. In case more than one instance is having the maximum, all of their names should be concatenated.
Let's say I have Class A as follows.
Class A {
...
String getName(){...}
int getValue1() {...}
int getValue2() {...}
...
int getValueN() {...}
...
}
I have a List<A> listToSort. I would normally sort this list as listToSort.sort(Comparator.comparing(A::getValue1)) or listToSort.sort(Comparator.comparing(A::getValue2)), so on and so forth. Then get the ones sharing the maximum value.
In a method I believe this should be done as:
String getMaxString (Comparator c) {
listToSort.sort(c);
...
}
and send Comparator.comparing(A.getValueX) as parameter to call it with different methods. (X here indicates an arbitrary number for the getValue function).
However, I need to also return other instances sharing the same values
I will need to pass the Class methods to my method and call on instances as:
String getMaxString (Comparator c) {
listToSort.sort(c);
int maxValue = listToSort.get(listToSort.size() - 1).getValueX();
String maxString = listToSort.get(listToSort.size() - 1).getName();
for (int i = listToSort.size() - 2; i >= 0; i--) {
if (listToSort.get(i).getValueX()() == maxValue) {
maxString += ", " + listToSort.get(i).getName();
}
}
return maxString;
}
How would I pass this method to call on instances here? Or do I need to consider another way?
Edit:
I have a list of Courses as List<Course> mylist where a course can be simplified as:
Class Course {
private String name;
private int capacity;
private int students;
...
//bunch of getters.
}
My task is to return Strings for the course(es) with maximum capacity, the course(es) with maximum registered students, the course(es) with most difficulty, the maximum filled percentage, the course(es) with the maximum number of TAs etc...
Edit 2:
As requested in the comment section.
List of
Course a (name "a", capacity 10, students 5)
Course b (name "b", capacity 20, students 5)
Course c (name "c", capacity 30, students 0)
Sorting based on capacity should return "c"
Sorting based on students should return "a b"
You can pass the getter method and create the Comparator in getMaxString:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
public class Foo {
static class AClass {
private final String name;
private final int value1;
private final int value2;
String getName() { return name; }
int getValue1() { return value1; }
int getValue2() { return value2; }
public AClass(String name, int value1, int value2) {
this.name = name;
this.value1 = value1;
this.value2 = value2;
}
}
static String getMaxString(Function<AClass,Integer> f, List<AClass> listToSort) {
listToSort.sort(Comparator.comparing(f));
int maxValue = f.apply(listToSort.get(listToSort.size() - 1));
String maxString = listToSort.get(listToSort.size() - 1).getName();
for (int i = listToSort.size() - 2; i >= 0; i--) {
if (f.apply(listToSort.get(i)) == maxValue) {
maxString += ", " + listToSort.get(i).getName();
}
}
return maxString;
}
public static void main(String[] args) {
List<AClass> list = new ArrayList<>();
list.add(new AClass("a", 1,2));
list.add(new AClass("b", 1,2));
list.add(new AClass("c", 2,1));
list.add(new AClass("d", 2,1));
System.out.println(getMaxString(AClass::getValue1, list));
System.out.println(getMaxString(AClass::getValue2, list));
}
}
As Tim Moore suggested above, it isn't necessary to sort the list (which has cost O(n*log n)), we can traverse it twice:
static String getMaxString2(ToIntFunction<AClass> f, List<AClass> listToSort) {
int maxValue = listToSort.stream().mapToInt(f).max().orElseThrow();
return listToSort.stream()
.filter(a -> maxValue == f.applyAsInt(a))
.map(AClass::getName)
.collect(Collectors.joining(", "));
}
Note that you should test your code with an empty list.
It's useful to look at the type signature for Comparator.comparing, because it sounds like you want to do something similar:
static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
The interesting part is the type of keyExtractor. Roughly speaking, it's a function from the type of the object you're comparing, to the type of the field you want to use for the comparison. In our case, these correspond to the A class and Integer. Because these types are fixed in this example, you can declare a method with a signature like this:
String getMaxString(Function<A, Integer> property)
With the existing algorithm, it can be used this way:
String getMaxString(Function<A, Integer> property) {
listToSort.sort(Comparator.comparing(property));
int maxValue = property.apply(listToSort.get(listToSort.size() - 1));
String maxString = listToSort.get(listToSort.size() - 1).getName();
for (int i = listToSort.size() - 2; i >= 0; i--) {
if (listToSort.get(i).getValueN() == maxValue) {
maxString += ", " + listToSort.get(i).getName();
}
}
return maxString;
}
However, it isn't necessary or efficient to sort the entire list in order to determine the maximum elements, as this can be determined by iterating through the list once:
String getMaxString(Function<A, Integer> property) {
int maxValue = Integer.MIN_VALUE;
StringBuilder maxString = new StringBuilder();
for (A element : listToSort) {
int currentValue = property.apply(element);
if (currentValue > maxValue) {
// there is a new maximum, so start the string again
maxString = new StringBuilder(element.getName());
maxValue = currentValue;
} else if (currentValue == maxValue) {
// equal to the existing maximum, append it to the string
if (maxString.length() > 0) {
maxString.append(", ");
}
maxString.append(element.getName());
}
// otherwise, it's less than the existing maximum and can be ignored
}
return maxString.toString();
}
Either way, you can call it using the same method reference syntax:
getMaxString(A::getValueN)
Time complexity O(n) - only one iteration through the dataset.
Hope it'll help.
If something will be unclear fill free to raise a question.
Main
public class MaxClient {
public static void main(String[] args) {
Comparator<A> comp = Comparator.comparingInt(A::getVal1);
List<A> items = List.of(new A(1, 8), new A(2, 8), new A(5, 8), new A(5, 27), new A(3, 8));
items.stream()
.collect(new GetMax(comp))
.forEach(System.out::println);
}
}
Custom collector GetMax
public class GetMax implements Collector <A, Deque<A>, Deque<A>> {
private final Comparator<A> comp;
public GetMax(Comparator<A> comp) {
this.comp = comp;
}
#Override
public Supplier<Deque<A>> supplier() {
return ArrayDeque::new;
}
#Override
public BiConsumer<Deque<A>, A> accumulator() {
return (stack, next) -> {
if (!stack.isEmpty() && comp.compare(next, stack.peekFirst()) > 0) stack.clear();
if (stack.isEmpty() || comp.compare(next, stack.peekFirst()) == 0) stack.offerLast(next);
};
}
#Override
public BinaryOperator<Deque<A>> combiner() {
return (stack1, stack2) -> {
if (stack1.isEmpty()) return stack2;
if (stack2.isEmpty()) return stack1;
if (comp.compare(stack1.peekFirst(), stack2.peekFirst()) == 0) {
stack1.addAll(stack2);
}
return stack1;
};
}
#Override
public Function<Deque<A>, Deque<A>> finisher() {
return stack -> stack;
}
#Override
public Set<Characteristics> characteristics() {
return Set.of(Characteristics.UNORDERED);
}
}
Class A that I used for testing purposes
public class A {
private int val1;
private int val2;
public A(int val1, int val2) {
this.val1 = val1;
this.val2 = val2;
}
public int getVal1() {
return val1;
}
public int getVal2() {
return val2;
}
#Override
public String toString() {
return "A val1: " + val1 + " val2: " + val2;
}
}
OUTPUT
A val1: 5 val2: 8
A val1: 5 val2: 27
Thanks for posting the information I requested. Here is what I came up with.
Create a list of Course objects
List<Course> list = List.of(
new Course("a", 10, 5),
new Course("b", 20, 5),
new Course("c", 30, 0));
Stream the methods and apply them to the list
List<String> results = Stream.<Function<Course, Integer>>of(
Course::getCapacity,
Course::getStudents)
.map(fnc-> getMaxString(fnc, list))
.toList();
results.forEach(System.out::println);
print the results
c
a b
I wrote a simple method that takes a method reference and list and finds the maximum. It does not do any sorting.
allocate a list to hold the names
set the maximum to the lowest possible
iterate thru the list applying the method.
if the value is greater than the current max replace it and clear the current list of names.
otherwise, if equal, add a new name.
once done, return the formatted string.
static String getMaxString(Function<Course, Integer> fnc,
List<Course> list) {
List<String> result = new ArrayList<>();
int max = Integer.MIN_VALUE;
for (Course obj : list) {
int val = fnc.apply(obj);
if (val >= max) {
if (val > max) {
result.clear();
}
max = val;
result.add(obj.getName());
}
}
return String.join(" ", result);
}
this is my first class for the Crackerpacker,i have created another class for the arraylist and then added all these cracker packers to that arraylist , now i want to sort the arraylist in descending order of number of boxes packed by the CrackerPackers
public class CrackerPacker {
private String name;
private int numberOfBoxes;
public CrackerPacker (String name, int numberOfBoxes){
this.name = name;
this.numberOfBoxes = numberOfBoxes;
}
public int getNumberOfBoxes() {
return numberOfBoxes;
}
public void setNumberOfBoxes(int numberOfBoxes) {
this.numberOfBoxes = numberOfBoxes;
}
public String getName() {
return name;
enter code here
}
#Override
public String toString() {
return "CrackerPacker{" +
"name='" + name + '\'' +
", numberOfBoxes=" + numberOfBoxes +
'}';
}
public double getwage() {
if ( numberOfBoxes < 51 ) {
return numberOfBoxes * 1.15;
}
else {
double wage = (57.5 + ((numberOfBoxes - 50) * 1.25));
return wage;
This is the second class in which i have created an arraylist to add all the crackerpacker objects in it
this arraylist i want sort in descending order of the number of boxes packed by the CrackerPackers
import java.util.ArrayList;
import java.util.Collections;
public class Common {
public static ArrayList<CrackerPacker> lectures = new ArrayList<>();
int sum = 0;
int boxes = 0;
public int totalWage() {
for (int i = 0; i < lectures.size(); i++) {
sum += lectures.get(i).getwage();
}
return sum;
}
public int totalBoxes() {
for (int i = 0; i < lectures.size(); i++){
boxes += lectures.get(i).getNumberOfBoxes();
}
return boxes;
This my main method , i want sort the array list in descending roder of the number of boxes packed by the objects , please show me how can do it
import java.util.Collections;
public class Main {
public static void main(String[] args) {
CrackerPacker steve = new CrackerPacker("STEVE", 127);
CrackerPacker gary = new CrackerPacker("Gary", 103);
CrackerPacker tony = new CrackerPacker("tony", 473);
CrackerPacker saad = new CrackerPacker("Saad", 129);
CrackerPacker rubiya = new CrackerPacker("rubiya", 117);
Common common = new Common();
Common.lectures.add(steve);
Common.lectures.add(gary);
Common.lectures.add(tony);
Common.lectures.add(saad);
Common.lectures.add(rubiya);
for ( CrackerPacker element : Common.lectures) {
System.out.println(element);
}
System.out.println(common.totalWage());
System.out.println(common.totalBoxes());
i think i have use the compareto method but i dont knwo how to implement it
The below compares the numberOfBoxes of the passed CrackerPacker with that of the current objects. This will have the effect of sorting it in descending order.
public class CrackerPacker implements Comparable<CrackerPacker> {
#Override
public int compareTo(CrackerPacker c) {
return Integer.compare(c.numberOfBoxes, this.numberOfBoxes);
}
}
From the javadoc,
Compares this object with the specified object for order. Returns a
negative integer, zero, or a positive integer as this object is less
than, equal to, or greater than the specified object.
Hence, when comparing two CrackerPacker objects we are reversing the Integer compare check to achieve a descending ordering.
Example: Comparing 5 and 10, we are comparing 10 with 5 and hence Integer.compare returns 1 (since 10 > 5). But from outside comparison, we are stating that the first object is bigger than the second and hence it will appear after the second (..10....5...). This sorts the list in descending order.
The following expression will help
Collections.sort(Common.lectures, (Comparator< CrackerPacker >) (o1, o2) -> Integer.compare(o1.getNumberOfBoxes(), o2.getNumberOfBoxes()));
I have an array like below.
String [] arr = [ "0.1 IN", "0.2 IN", "1.4 IN" , "1.04 IN", "2.4 IN" , "0.01 IN", "11.4 IN" ];
How can I sort this kind of array ? Kindly suggest me a solution
There is a sort method in Arrays that takes a comparator:
static <T> void sort(T[] a, Comparator<? super T> c)
You could define your own Comparator like this:
Comparator<String> compareLengths = new Comparator<String>() {
#Override
public int compare(String left, String right) {
... parse the strings to come up with some kind of number
... return -1 if the left number < the right number, 0 if the
... left number = the right number, 1 if the left number > the
... right number
}
}
and use compareLengths as the second argument to sort.
If you could be sorting a large number of strings, this is not the most efficient way to do it, because you'll be parsing the same strings multiple times. So if the amount of data could be large, you're better off defining a new type that implements Comparable:
class Length implements Comparable<Length> {
private double length; // in inches, or whatever common unit you choose
private String lengthString;
public Length(String lengthString) {
this.lengthString = lengthString;
this.length = // parse the string and come up with a number
}
// getters
#Override
public compareTo(Length other) {
return Double.compare(this.length, other.length);
}
}
Create a new Length[] by constructing a new Length from each element in your String[], and sort that.
Note: not tested, and I could have gotten some details wrong...
Note 2: I'm assuming that the strings in your question are length measurements, and that you want to compare them as if comparing actual numeric values. But if you were looking for some other kind of comparison, either of the above two solutions should still work if you adapt it to your situation.
You can use this code for sort your array values according to the decimal values.
public class Test
{
public static void main(String[] args)
{
String[] arr = { "0.1 IN", "0.2 IN", "1.4 IN", "1.04 IN", "2.4 IN", "0.01 IN", "11.4 IN" };
ArrayList<SortHelper> list = new ArrayList<SortHelper>();
//separate numbers and words
for (String value : arr)
{
SortHelper sh = new SortHelper();
sh.setNumer(Double.valueOf((value.split(" "))[0]));
sh.setWord((value.split(" "))[0]);
list.add(sh);
}
//compare according to decimal value
Collections.sort(list);
}
/**
* Helper class for sorting the string values in your array.
*
*/
public static class SortHelper implements Comparable
{
private double numer;
private String word;
public SortHelper()
{
}
public double getNumer()
{
return numer;
}
public void setNumer(double numer)
{
this.numer = numer;
}
public String getWord()
{
return word;
}
public void setWord(String word)
{
this.word = word;
}
/**
* compare according to your string value decimal number.
*/
#Override
public int compareTo(Object obj)
{
SortHelper sh = (SortHelper) obj;
if (this.getNumer() == sh.getNumer())
{
return 0;
}
else if (this.getNumer() > sh.getNumer())
{
return 1;
}
else
{
return -1;
}
}
}
}
I have a bean called vulnerability. It is having a column "severity".
private String severity;
Severity can hold string value High,Medium and Low. Now whenever sorting of this bean on the basis of severity column is done it happens alphabetically i.e. High,Low and Medium. But i want the sorting to happen high,medium, low when descending and low, medium,high when ascending.
I was seeing comparator to make this custom sorting but it needs to cover lots of cases. Isn't their any other way?
You can (and should) use an enum - not a String nor a int:
enum Severity {
LOW, MEDIUM, HIGH;
}
Usage:
List<Severity> lst = new ArrayList<Severity>();
lst.add(Severity.MEDIUM);
lst.add(Severity.LOW);
lst.add(Severity.HIGH);
for (Severity s : lst)
System.out.println("s = " + s);
Collections.sort(lst);
System.out.println();
for (Severity s : lst)
System.out.println("s = " + s);
OUTPUT:
s = MEDIUM
s = LOW
s = HIGH
s = LOW
s = MEDIUM
s = HIGH
EDIT
Since the OP says he can't modify the usage of Strings, we can map the strings into a comparable values:
static Map<String, Integer> severities = new HashMap<String, Integer>();
static {
severities.put("LOW",1);
severities.put("MEDIUM",2);
severities.put("HIGH",3);
}
public static void main(String[] args) {
List<String> lst = new ArrayList<String>();
lst.add("MEDIUM");
lst.add("LOW");
lst.add("HIGH");
for (String s : lst)
System.out.println("s = " + s);
Collections.sort(lst, new Comparator<String>() {
public int compare(String a1, String a2) {
Integer v1 = severities.get(a1);
Integer v2 = severities.get(a2);
return v1.compareTo(v2);
}
});
System.out.println();
for (String s : lst)
System.out.println("s = " + s);
}
and if you want to order the items in descending order you can sort and then reverse:
Collections.sort(lst);
Collections.reverse(lst);
There is an implicit compareTo operator defined on enums, which takes their declaration order to mean "smaller than". No additional code is needed.
enum Severity { Low, Medium, High }
Low.compareTo(High); // returns -1
Medium.compareTo(Low); // returns 1
However, note that the names of the enum constants will be those printed by toString() (and therefore visible to users if you echo enums directly) - if you want to use different internal and external names, possibly to uphold code conventions (say, all-caps-constants), then you will need to add an enum constructor and override the enum's toString method to use the passed-in constructor attribute.
If you cannot use enums, and you cannot change your bean
Then build a Comparator for it:
public class SeverityComparator implements Comparator<String> {
private int direction;
public SeverityComparator(boolean reverse) {
this.direction = reverse ? -1 : 1;
}
private int severity(String s) {
if (s.equals("Low")) { // you really should have constants for the values...
return 0;
} else if (s.equals("Medium")) {
return 1;
} else if (s.equals("High")) {
return 2;
} else {
throw new IllegalArgumentException("Not a severity: " + s);
}
}
#Override
public int compareTo(String other) {
return direction * (severity(this) - severity(other));
}
}
Use as
Collections.sort(listOfSeverities, new SeverityComparator(false)); // ascending
Collections.sort(listOfSeverities, new SeverityComparator(true)); // descending
#alfasin answer is correct but i would suggest using guava's Ordering:
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import javax.annotation.Nullable;
import java.util.List;
public class SeveritySortTest {
private static final List<Severity> SEVERITY_LIST = ImmutableList.copyOf(Severity.values());
public static void main(String[] args) {
Ordering<Severity> severityOrdering = Ordering.natural().onResultOf(new Function<Severity, Integer>() {
#Nullable
#Override
public Integer apply(#Nullable Severity input) {
return input.getSeverity();
}
});
List<Severity> sortedAscending = severityOrdering.sortedCopy(SEVERITY_LIST);
List<Severity> sortedDescending = severityOrdering.reverse().sortedCopy(SEVERITY_LIST);
}
enum Severity {
LOW(1), MEDIUM(2), HIGH(3);
private int severity;
Severity(int s) {
severity = s;
}
int getSeverity() {
return severity;
}
}
}
Working Solution:
Collections.sort(recommendations, new Comparator() {
private int priority(String s) {
if (s.equalsIgnoreCase("Low")) {
return 1;
} else if (s.equalsIgnoreCase("Medium")) {
return 2;
} else if (s.equalsIgnoreCase("High")) {
return 3;
} else {
return 0;
}
}
#Override
public int compare(Recommendation o1, Recommendation o2) {
return -1 * (priority(o1.getPriority()) - priority(o2.getPriority()));
}
});
If you want the DB to do this through JPA/Hibernate you could create a sort expression based on a simple case statement, assuming your entity is called Case:
Expression exp = criteriaBuilder.selectCase(root.get(Case_.priority)).when("High", 1).when("Medium", 2).otherwise(3);
queryBuilder.orderBy(orderDir.isAscending() ? criteriaBuilder.asc(exp) : criteriaBuilder.desc(exp));
Using case statements in an order by clause isn't great for performance, but solves it. Works with Oracle.
With this code I get this output:
TreeSet<String> t=new TreeSet<String>();
t.add("test 15");
t.add("dfd 2");
t.add("ersfd 20");
t.add("asdt 10");
Iterator<String> it=t.iterator();
while(it.hasNext()){
System.out.println(it.next);
}
I get:
asdt 10
dfd 2
ersfd 20
test 15
How can I get an order of this kind, based on the numbers, with TreeSet?
dfd 2
asdt 10
test 15
ersfd 20
The TreeSet implementation is sorting by the lexicographic order of the string values you insert. If you want to sort by the integer value, then you'll need to do as these others suggested and create a new object and override the compareTo method, or use your own comparator.
Set<String> set = new TreeSet<String>(new Comparator<String>() {
public int compare(String one, String other) {
// implement
}
});
or
public class MyClass implements Comparable {
private String key;
private int value;
public int compareTo(MyClass other) {
// implement
}
public boolean equals(MyClass other) {
// implement
}
// snip ...
}
Set<MyClass> set = new TreeSet<MyClass>();
You can use one of the TreeSet constructors: http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#TreeSet%28java.util.Comparator%29
This allows you to specify your own comparator that allows you to organize the entries in the Set however you like.
Implement a Comparator that extracts the number from the String and then sorts by the number first, only falling back to a String comparison if both numbers are equal.
Use the TreeSet constructor that receives a custom Comparator, and implement a Comparator that sorts the string differently.
Here's an example (untested, check the code before using):
TreeSet<String> t = new TreeSet<String>(new Comparator<String>() {
public int compare(String s1, String s2) {
int spaceIndex1 = s1.indexOf(' ');
int spaceIndex2 = s2.indexOf(' ');
return Integer.parseInt(s1.substring(spaceIndex1 + 1)).compareTo(Integer.parseInt(s2.spaceIndex2 + 1));
}
});
Using lambda
Set<String> set = new TreeSet<String>(
(o1, o2) -> String.format("%3s", o1.substring( o1.indexOf(" ") + 1)).replace(" ","0")
.compareTo( String.format("%3s", o2.substring( o2.indexOf(" ") + 1)).replace(" ","0")
));
set.add("test 15");
set.add("dfd 2");
set.add("ersfd 20");
set.add("asdt 10");
set.stream().forEach(s -> System.out.println(s));
result:
dfd 2
asdt 10
test 15
ersfd 20
But I strongly recommend separe the significant values (in this case integers) in other key estucture. for easy manipulation.
Try this:
TreeSet set = new TreeSet(new Comparator<String>(){
public int compare(String o1, String o2){
String n1 = o1.split(" ")[1];
String n2 = o2.split(" ")[1];
return Integer.parse(n2) - Integer.parse(n1);
}
public boolean equals(String o1, String o2){
return compare(o1,o2)==0;
}
});
class Book implements Comparable<Book> {
String name;
int id;
public Book(String name,int id) {
this.name = name;
this.id = id;
}
public int compareTo(Book b) {
if(id>b.id){
return 1;
}else if(id<b.id){
return -1;
}else{
return 0;
}
}
}
public class TreeSet2 {
public static void main(String[] args) {
Set<Book> set=new TreeSet<Book>();
//Creating Books
Book b1=new Book("test", 15);
Book b2=new Book("dfd", 2);
Book b3=new Book("ersfd", 20);
Book b4=new Book("asdt", 10);
//Adding Books to TreeSet
set.add(b1);
set.add(b2);
set.add(b3);
set.add(b4);
//Traversing TreeSet
for(Book b:set){
System.out.println(b.name+" "+b.id);
}
}
}
import java.util.ArrayList;
import java.util.*;
import java.util.Arrays;
class SecondHighest {
public static void main(String[] args) {
int i;
int a[]={2,3,4,5,7,6,9,9,9,8,8,7};
int total=a.length;
Arrays.sort(a);
TreeSet<Integer> set=new TreeSet<Integer>();
for(i=0;i<total;i++)
{
set.add(a[i]);
}
System.out.println(set.last()-1);
Iterator<Integer> itr=set.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
This is a program related to find the second largest element in array. I have used Tree-set for sorting purpose. Using tree-set we can remove all the repeated elements.
After sorting element using set method.There is a function set.last() by which you can find the last element of array or list.
I applied set.last()-1 function that gives me second largest element in array.