comparator

Sorting list of list of elements of a custom class in R?

烈酒焚心 提交于 2019-12-01 23:58:06
问题 I have a custom class object (list of tuples). I have defined <.myclass >.myclass and ==.myclass on it as well. Now I have a a <- obj1 # of myclass b <- obj2 # of myclass c <- obj3 # of myclass L <- list(list(a,12,1),list(b,215,23),list(c,21,9)) I want to sort L, on index 1. i.e. I have b < c < a then, I want sorted L in this form list(list(b,215,23),list(c,21,9),list(a,12,1)) How do I achieve this? In my searches, I found how to sort on particular index, and using that I wrote the following

Comparator won't work with Arrays.sort

十年热恋 提交于 2019-12-01 23:14:03
问题 So I'm working on a comparator problem and I can't figure out why the Array.sort in this first class is giving me the error of: The method sort(T[], Comparator) in the type Arrays is not applicable for the arguments (ArrayList, CalorieComparator) Restaurant Class: import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class Restaurant { private ArrayList<Edible> elist; public Restaurant() { } public void addEdibleItem(Edible item){ elist.add(item); } public List

Java sorting is not the same with MySQL sorting

孤街醉人 提交于 2019-12-01 22:47:38
问题 I need to check the sorting in the table and table content is given by MySQL. I'm using the following row to sort the list: Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER); And get the following result: tes3@test.com test4@test.com test5@test.com test@test.com test_user@mail.com user-og@driver.com And this is what I get from MySQL by query: SELECT 'email' FROM 'user' WHERE 1 ORDER BY 'user'.'email' ASC : tes3@test.com test_user@mail.com test@test.com test4@test.com test5@test.com user

Sorting list of list of elements of a custom class in R?

半腔热情 提交于 2019-12-01 21:58:09
I have a custom class object (list of tuples). I have defined <.myclass >.myclass and ==.myclass on it as well. Now I have a a <- obj1 # of myclass b <- obj2 # of myclass c <- obj3 # of myclass L <- list(list(a,12,1),list(b,215,23),list(c,21,9)) I want to sort L, on index 1. i.e. I have b < c < a then, I want sorted L in this form list(list(b,215,23),list(c,21,9),list(a,12,1)) How do I achieve this? In my searches, I found how to sort on particular index, and using that I wrote the following function magic_sort <- function(lst, sortind, dec = T) { return(lst[order(sapply(lst,'[[',sortind),

Comparator won't work with Arrays.sort

老子叫甜甜 提交于 2019-12-01 21:20:11
So I'm working on a comparator problem and I can't figure out why the Array.sort in this first class is giving me the error of: The method sort(T[], Comparator) in the type Arrays is not applicable for the arguments (ArrayList, CalorieComparator) Restaurant Class: import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class Restaurant { private ArrayList<Edible> elist; public Restaurant() { } public void addEdibleItem(Edible item){ elist.add(item); } public List<Edible> orderByCalories(){ Arrays.sort(elist, new CalorieComparator()); } CalorieComparator class: import

Collections sort not being accepted by Eclipse

半腔热情 提交于 2019-12-01 19:36:26
import java.io.BufferedReader; import java.util.Collections; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import com.javaranch.common.TextFileIn; public class SortNames { public static class CelebrityNamesFile { public String firstName; public String lastName; public static class CompareLastName implements Comparator< CelebrityNamesFile > { @Override public int compare( CelebrityNamesFile o1, CelebrityNamesFile o2 ) { return

Sorting ArrayList of Arraylist<String> in java

[亡魂溺海] 提交于 2019-12-01 17:56:38
I have an ArrayList of ArrayList of String. In Outer ArrayList on each index each Inner ArrayList has four items have four parameters. Contacts Id Contacts Name Contacts Adress Contacts Number Now I want to sort the complete ArrayList of the on the basis of Contact Name Parameter. Means I want to access the outer Arraylist and the inner ArrayList present on each index of outer Arraylist should be sorted according to contact Name. Comparator / Comparable Interfaces not likely to help me. Collection.sort can't help me Sorting Arraylist of Arraylist of Bean . I have read this post but it is for

Java - how to sort object in many ways: Arrays.sort(), Comparable<T>

北战南征 提交于 2019-12-01 17:53:47
Let's say that I have an array with objects, where I have some employees (objects). They all have: int age , double salary . I want to sort this array so my class implements Comparable <Employee> . I've made a method: public int compareTo(Employee other) { return Double.compare(salary, other.salary); } And it's ok, sorting is working fine. But I'm sorting by double salary . Now I want to sort by int age so what now? I've made a method: public int compareAge(Employee other) { return Integer.compare(age, other.age); } And how can I use Arrays.sort() with this? I want to have possibility to use

How to get inverse of a comparator in java

匆匆过客 提交于 2019-12-01 16:13:54
In a method I receive a generic object E extends Comparable<E> as an argument. Now i want to create two priority queues.One which uses the comparator used by E and other queue which uses the opposite of comparator used by E(i.e. if E uses '<' then second queue must use '>='). Please hep me how to create two such queues. queue2=new PriorityQueue<E>(0,Collections.reverseOrder(e)); I am getting the error that reverseOrder is not applicable. please help Look at Collections.reverseOrder . Your object E extends java.lang.Comparable, but it is not a java.util.Comparator . Create your first queue w/o

How can I search an std::map using a key of a different type

怎甘沉沦 提交于 2019-12-01 15:23:28
If I have std::map<X, Blah> , what is the best way of looking up a matching item in the map using an instance of Y ? Assume the information in Y is enough to uniquely find an X , but for performance reasons I don't want to create an instance of X by copying Y values. I realize I can do this by creating a common base class or interface for X and Y and making that the map key, but is there any other way? e.g. creating some sort of comparator object? Here is the sample code for clarity: class X { public: int id; int subId; }; std::map<X, Details> detailsMap; class Y { public: int getId(); int