Linked Questions
62 questions linked to/from How to use Comparator in Java to sort
0
votes
5answers
2k views
Sort LinkedList in Java [duplicate]
I have a List:
LinkedList<Student> student = new LinkedList<Student>();
In the Student class, it has an attribute String name. I want to sort the List students by its name.
Is there any ...
0
votes
1answer
164 views
Sort an ArrayList of objects but with a changing sorting key [duplicate]
I am implementing the median-cut algorithm for an image and I have an ArrayList with all my pixels but now I need to sort it based on one color channel. Here is my Pixel class.
public class Pixel ...
0
votes
1answer
60 views
How would I sort given this code [duplicate]
I also have an ArrayList items. I have classes derived from Media. Given the code below, how would I sort the arraylist by duration? e.g.
Collections.sort(myMedia, ?);
Here is the class
import java....
0
votes
1answer
87 views
How ro sort the array list in ascending order based on GPA? The arraylist is holding an array of objects. (Java) [duplicate]
I'm trying to sort this arraylist based on GPA in ascending order. Student class encapsulates 3 pieces of information: Student Number, Last Name, Grade Point Average.
So I need to sort them using ...
-6
votes
1answer
92 views
What Java compareTo function does [duplicate]
I'm migrating a project from Java code to PHP, but some Java code is Alien to me. May i know what this Java code does? And how can i "interpret" this in PHP?
//contentList array
contentsList.sort((...
0
votes
0answers
23 views
Is there a way to sort a list by the degree of detail of objects? [duplicate]
I would like to sort a list by the degree of detail of the objects. The most detailed object should then be at the top of the list.
example:
there is a class "position" with 4 attributes (latitude, ...
498
votes
27answers
880k views
When do you use Java's @Override annotation and why?
What are the best practices for using Java's @Override annotation and why?
It seems like it would be overkill to mark every single overridden method with the @Override annotation. Are there ...
620
votes
15answers
205k views
What is a raw type and why shouldn't we use it?
Questions:
What are raw types in Java, and why do I often hear that they shouldn't be used in new code?
What is the alternative if we can't use raw types, and how is it better?
118
votes
17answers
201k views
Java ArrayList - how can I tell if two lists are equal, order not mattering?
I have two ArrayLists of type Answer (self-made class).
I'd like to compare the two lists to see if they contain the same contents, but without order mattering.
Example:
//These should be equal.
...
107
votes
19answers
105k views
When to use Comparable and Comparator
I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works.
Now looking back at this, ...
114
votes
10answers
169k views
Sorting an ArrayList of objects using a custom sorting order
I am looking to implement a sort feature for my address book application.
I want to sort an ArrayList<Contact> contactArray. Contact is a class which contains four fields: name, home number, ...
78
votes
5answers
13k views
Java Integer compareTo() - why use comparison vs. subtraction?
I've found that java.lang.Integer implementation of compareTo method looks as follows:
public int compareTo(Integer anotherInteger) {
int thisVal = this.value;
int anotherVal = anotherInteger....
51
votes
8answers
115k views
How to sort a Collection<T>?
I have a generic Collection and am trying to work out how I can sort the items contained within it. I've tried a few things but I can't get any of them working.
8
votes
2answers
18k views
sort arraylist of complex objects alphabetically
I know that Collections.sort(myArrayList) can sort an arraylist alphabetically when they are strings, but what about when they are something more complex such as a data object containing two or more ...
7
votes
4answers
2k views
I want to use Collections.sort() twice on two different parameters [duplicate]
I have a list of objects that I want to sort using different properties.
@Override
public int compareTo(Object obj)
{
Field tab = (Field) obj;
int objx = Integer.parseInt(tab.getX());
// ...
2
votes
7answers
1k views
How can we pass a variable field /method name in Comparator.comparing
I have a Report {String name, Date date, int score } class.
I want to be able to sort a list of reports for any member variable using the new java 8 syntax
So java 8 provides this new
list.sort(...
0
votes
3answers
4k views
LinkedHashSet to order elements in descending order?
I have a set declared as follows
Set<String> orderSet = new LinkedHashSet <String>();
The String values stored are a combination of date and time convention
orderSet.add(bean.getDate()+...
2
votes
2answers
4k views
p:column in p:dataTable, “sortBy” descending on the first click
The sortBy attribute of <p:column> in <p:dataTable> lets the user to sort a column ascending or descending on each click, is there anyway to sort column descending on the first click of ...
0
votes
5answers
659 views
Unable to understand Arrays.sort() functionality
I'm new to Java, and I'm trying to understand Comparator Interface.
I tried the following code:
package comparator;
import java.io.*;
import java.util.*;
public class comparator {
public ...
0
votes
3answers
1k views
Maximum value in array containing Objects Java
I have an array named trick of type Card which stores the Suit and the Rank.
Is there a way to use Collections.max to find the highest ranked card? I have used ordinals in other aspects of my code but ...
0
votes
1answer
3k views
How to read objects from binary file to ArrayList in java?
I'm Java beginner. I have to write some objects in binary file and than be able to write next object at the end of file and in second case in chronogical order.
My code right is looks like this:
...
3
votes
3answers
545 views
Writing a comparator that sorts based on multiple fields
I have 3 data fields, let's name them Field 1, Field 2, Field 3
Now let's say I have these values:
Field 1 Field 2 Field 3
1 3 4
2 3 3
3 3 5
...
2
votes
4answers
527 views
Sort List based on conditions (ascending or descending)
I wanted to sort the list based on two properties but on certain conditions.
The below class has two properties - memberid,membernumber. And the values are as below.
obj ()
{
memberid;
...
4
votes
6answers
137 views
ComperatorFactory using static methods or each in its own class?
I was thinking of implementing multiple Comperators. Now I am not sure about how to do this in a most favourable way, i.e. still having the ability to do dependency injection.
Method 1: One class is ...
1
vote
1answer
1k views
How To implement Compare to method for custom object
In java i have an arrayList of my own(implemented) i have a sort method so i need 'compareTo' method thats why i implemented Comparable and made the class abstract. But if i use arraylist from main ...
0
votes
3answers
960 views
How do I sort this array list in java
I am using OpenCsv to access a fie which I wish to sort and then write back to another file name. Am having problem sorting the list. Thanks.
Am trying to sort the csv file by the 1st and 2nd ...
0
votes
3answers
335 views
Sort an Arraylist multiple times [closed]
I have one class "SendBundleQuery" which contains some objects like category , Type , name. category can be of two types base and Addons . Type can be of three types data, voice , sms. I have a ...
1
vote
2answers
114 views
Sorting a String in java
i'm trying to sort a string in java, below is my code:
public class SortingString {
public static void Sortstring(){
String str="String";
char [] charStr = str.toCharArray();
...
0
votes
3answers
660 views
Comparing Characters in ArrayLists?
I am trying to populate a RecyclerView with a custom adapter. This Adapter takes in a model that has setters and getters. This model contains three parameters: String username, int proPic, and int ...
3
votes
2answers
71 views
Customized sort on list of objects
I have list of objects like
actors = [Person('Raj' ,'Hindi'),
Person('John', 'English'),
Person('Michael' 'Marathi'),
Person('Terry','Hindi'),
Person('...
0
votes
1answer
466 views
javafx line chart sorted data
I'm trying to sort data string on both axis but it doesn't work.
The code is below :
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final Axis<String> yAxis = ...
0
votes
4answers
59 views
Trying to learn generics, is there a way to see if one generic object is bigger or smaller then another?
I thought it would be nice to use generics to write a quick sort. I'm just learning generics, and it seems like you can only test whether one generic value is equal to another. Is there a way to see ...
0
votes
2answers
462 views
data structure that holds two different types datatypes java
I have an object.
public class Test{
int age;
LocalDateTime currentTime;
//class also contains getters and setters.
}
Lets assume I have a List and i need to calculate the mean age. I iterate ...
1
vote
3answers
259 views
porting C++ sort function to Java
I have a problem porting c++ code to Java. I have a list of pair as follows
vector<pair<pair<int,int>,int> > M;
I wrote the equivalent in Java as follows
List<Pair<Pair<...
0
votes
2answers
248 views
How to understand the order induced by the comparator in Java?
I was so confused about comparator and Collections.sort() in Java.
I don't understand the order induced by the comparator. I don't clear about which number the compare function should return to get ...
-3
votes
2answers
406 views
Multiple comparators in a class
I am hard stuck on a problem I cannot find a good answer to. I've found
this one about custom comparators, but it is incomplete:
class YourClass {
static Comparator<YourClass> ...
-2
votes
2answers
309 views
Sort an ArrayList of Points in page line order
I need to order an ArrayList of Points in the page line order. Assume these points as minX and minY of Rectangle of text.
Say like I have points like below
Point(100,10), Point(110, 20), Point(125, ...
0
votes
3answers
303 views
How to get unique rows using SET class from an Arraylist of “Arraylist string objects” of Type Setters & Getters class
I need your help in java code such as how can I get unique records from an arraylist which is multidimensional array of casting a class of Value objects(setters and getters).
I'm reading a table and ...
-3
votes
1answer
386 views
Java Comparator - Two level comparison on object
I am using java Comparator which need sorting based on below conditions -
Object1 - name subName
Object2 - name subName
Compare Object1 & Object2 based on below condition-
condition 1:...
1
vote
1answer
85 views
I am beginner in android Java. Can anyone explain the code
This code is written to sort the hashmap monthwise but i am not able to understand completely. Can anyone explain this. Also i want to know if i am accessing hashmap correctly in for loop. ...
0
votes
2answers
63 views
Comparing one variable only between two instance
i'm studying Java and they gave me an exercise which i'm really struggling with. I'll give you the full exercise (it's translated from my native language) instead of explaining it, so you can ...
1
vote
3answers
174 views
Getting an ArrayList
I would like to ask if how can I print out my arraylist by using map.get().get()? The output should be:
"Aguinaldo, Marcos, Quezon"
And the way to print it should be:
System.out.println(map.get("...
0
votes
1answer
89 views
How to sort ArrayList of classes
I am trying to sort arrayList of classes which have a abstract type.
Here is my interface
public interface iSomeInterface {
public int level();
}
There are couple of implementation of this ...
1
vote
1answer
65 views
JAVA: How to sort list on the base of the field [duplicate]
I have the following list and I want to sort this whole list on the basis of the date how can I do this?
List<DevicePing> devicePing=Arrays.asList(
new DevicePing(1L,2017-11-21 ...
1
vote
2answers
76 views
Sort an arraylist by an object's attribute or method *but by different sort orders = ABC, BCA, CAB, etc
I have a list which contains items which have a variable which contains which may be 1 of 3 different values.
I want to be able to sort the list according to the value given so that the list will end ...
0
votes
2answers
75 views
How to set Custom listview According to the itemname alphabetically?
This is the method where i am getting the data from the server and show in the listview
I want to show the list alphabetically accordingly to the i am setting the name with the help of model class. ...
0
votes
1answer
102 views
how to check and filter for group of different values in java
I have a text file with the data as shown below as an example..
2;30;1;801;2;1951;195102;1111;
2;30;1;801;3;1621;162101;1111;
2;30;1;802;1;1807;180702;1111;
2;30;1;802;1;1807;180703;1111;
2;30;1;802;...
0
votes
3answers
99 views
How to sorting List<Object[]> in java
May I know how can I sort a List<Object[]> and order by more than one element ?
below is my code:
I retrieve my result from database using below code
public List<Object[]> readyToPrint()...
0
votes
2answers
83 views
For-loop is executed only once?
I'm implementing Insertionsort for university. My code works in theory, but my for-loop is executed only once instead of books.size() (which is 5, I've tested that). I tried it using the number 5, but ...
0
votes
2answers
36 views
How can I assign data from a file to an array
I have a file, which looks like follows:
Barack Obama 3
Michael Jackson 1
Dua Lipa 2
... all of 15 lines
I need to read from a file and place names in a one-...