Sort « comparator « Java Collection Q&A

Java Collection Q&A
3.Array Byte
4.Array Char
5.Array Convert
6.Array Dimension
7.Array Integer
8.Array Object
9.Array String
14.Garbage Collection
Java Collection Q&A » comparator » Sort 

1. Why is my simple comparator broken?

I have a class, which I have simplified to this:

final class Thing {
    private final int value;
    public Thing(int value) {

2. Java: fillings two vectors and sorting them

How can I fill up two vectors and combine them with eachother (either index or an object type in class). I guess if I use index to combine them, then the ...

3. Nice general way to sort nulls to the bottom, regardless?

I'm writing some custom Comparators, and I'd like them to push null items to the bottom of the list, regardless of whether I'm sorting ascending or descending. What's a good ...

4. How do I sort enum members alphabetically in Java?

I have an enum class like the following:

public enum Letter {


5. How to use Comparator in Java to sort

I learned how to use the comparable but I'm having difficulty with the Comparator. I am having a error in my code:

Exception in thread "main" java.lang.ClassCastException: New.People cannot be cast ...

6. java comparator for simple custom logic

How I can find line 4,1 and 6 in example below?
And is the use of Collection.sort() with Comparator reasonable in this case?

       a -  ...

7. Why I can't use Comparator to sort primitives?

As Java 5 have autoboxing, why I can't use Comparator to sort primitives? An int wouldn't be wrapped into a Integer?

8. java sorting with comparator and swap function

I need to sorting function with custom comparator and swap function. I can write one myself, but I'm wondering if someone else didn't already do it. Java runtime contains many specialized ...

9. Using comparator to make custom sort

I want to develop one sorting demo for car list. i am using data table to display car list. Now actually i want to sort the list by car color. here ...

10. How do I fix a Java Priority-Queue to correctly sort by a specific property?

I have a Java PriorityQueue for sorting objects from a specific class I made called Node. I want it to sort the Nodes by their getData() method. I tried the following ...

11. How to do this code using Comparator?

Sorry, I don't work with Java since a long time. I have a class which sort int type, and I am trying to do this, but this does not compile:

MySort.sort(vectorOfInt, new ...

12. java apache common lazylist sort with null values

Hallo, I am using the apache common LazyList (org.apache.commons.collections.list.LazyList). Now I'd like to call Collections.sort on this list, but I'm not sure if this is always allowed. This kind of list produces a ...

13. How to use Java comparator properly?

If I have the following class:

public class Employee {
    private int empId;
    private String name;
    private int age;

    public ...

14. How to ignore certain values using Java comparator for sorting

I'm using Java Comparator for sorting in my code. How I can only sort by certain values, e.g., the values range from 1 to 1000, (default is 1000), but I only ...

15. Does sorting by a non transitive comparator "work"?

What will happen if I supply a non-transitive Comparator to Collections.sort? Can I run into infinite loop? A small test I wrote produced an output, but I want ...

16. How write universal comparator which can make sorting through all necessary fields?

I needed to implement sorting on all fields of my class I wrote a comparator for each field in my class. But I had to write a individual comparator for each field. I ...

17. Collator plus Comparator

I have a collection of beans OptionItemDTO (properties label and value) and I'm currently using something like this to sort by label:

Collections.sort(combo, new LabelComparator())
The problem is that it does not sort ...

18. How to sort existing TreeSets with a Comparator?

The Collections class can be used to sort Lists with a Comparator. The Arrays class can do the same for arrays. Is there a way to sort a TreeSet according to a different Comparator after it's already been created? Here is some sample code to help make clear what I'm asking. import java.util.Comparator; import java.util.Set; import java.util.TreeSet; public class TreeSetsAndComparable { ...

19. Sorting wih Comparator dont work ?

I have a class that sorts with comprator, but i dosent work ! Any good ideas, look at my code ? Is there a better way to sort my list with objects ? package dk.jyskebank.jyskeinvest.backingbean.todayshighandlow; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.List; import dk.jyskebank.j2ee.core.dao.DataAccessObject; import dk.jyskebank.j2ee.core.exception.JyskeSystemException; import dk.jyskebank.jyskeinvest.backingbean.BackingBeanHelper; import dk.jyskebank.jyskeinvest.backingbean.BackingBeanHelperImpl; import dk.jyskebank.jyskeinvest.dao.JdbcDaoFactory; import dk.jyskebank.jyskeinvest.dao.funds.FundDao; import dk.jyskebank.jyskeinvest.domain.funds.Fund; public class ...

20. secondary sort using Comparator

Hello. if anybody has a minute, i can't seem to get this sort working. i am sorting vector elements and want to use the date as the primary sort and type as a secondary sort. i thought i'd try something like this... String type="widget"; String date_string="25-MAR-2005"; SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); Date date_object = dateFormat.parse(date_string); long time_long=date_object.getTime(); String comparison_string=time_long+type; then compare ...

22. sort by 2 fields using comparator

Hello people! Here is my problem: I'm trying to sort arrays of these objects, Hotlap from the implementation below. They describe drivers' laps in a race qualify. I assume that the fastest driver is the one that spent less time to finish a lap, represented by totalTime. The point is: when they draw in totalTime, the fastest is the one with ...

23. How to Use Comparator or BeanPropertyUtil to sort by multiple criteria

Hi All: I have an class that has attributes: id, name age Is there a way to use the Java comparator to sort by age first, than with those employee with the same age sort by name and than those employees with the same name sort by Id using the comparator method in Java. Or does another has example or ...

24. Sorting results using custom comparator

25. sorting by Comparator

26. Sorting using comparator

in my class i m using an array list that contain objectss(suppose of Songs class) now i want to sort this class that is in array list in some order . what i m doing is , sorting by implementing Comparator interface. now but the thing is this song class contain a hashMap also and based on the key in hashmap ...

27. Sorting a combination of two longs using a comparator

Hello to all, I need to implement a comparator that will take as input an object containing two longs, combine them and sort them in the form a.b, where a is the first long and b the second, first by a and then by b. (eg.1.2, 2.8, 1.5, 2.4, 3.4, 3.3, etc... becomes 1.2, 1.5, 2.4, 2.8, 3.3, 3.4, etc..) Is ...

28. Comparator sort

if(eDate1 == null || eDate1.equals("") || eDate2 == null || eDate2.equals("")) return ((String )stDate2 ).compareTo( (String)stDate1) ; else if ((arg0.getStartDate().equals(arg1.getStartDate()))) return -( (String )eDate1).compareTo( (String)eDate2 ); else return -( (String )stDate1).compareTo( (String)stDate2); } } If the input is: UnlawfulInfo ul1 = new UnlawfulInfo("20050601", "20050630"); UnlawfulInfo ul2 = new UnlawfulInfo("20050601", ""); UnlawfulInfo ul3 = new UnlawfulInfo("20050701", "20050725"); UnlawfulInfo ul5 = new UnlawfulInfo("20050701", ...

29. Help with Sorting with Comparator

30. Help with Sorting with Comparator  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.