Sort vector of custom objects

How can I sort the vector of my custom object and choose which property to sort by?

I saw this question and answer, but I'm not too sure what its sorting is. Sample code is preferable to "methodology".

Sort vector of custom objects

public class ItemLocation {
    String icon;
    String title;
    String message;
    String subtext;
    String deviceId;
    double latCoords;
    double lngCoords;
    int expiary;
    int id;
    double proximity;
    String locSeen;
}
+3
source share
4 answers

The following is an example that allows you to sort by a specific ItemLocation field:

public void sort(final String field, List<ItemLocation> itemLocationList) {
    Collections.sort(itemLocationList, new Comparator<ItemLocation>() {
        @Override
        public int compare(ItemLocation o1, ItemLocation o2) {
            if(field.equals("icon")) {
                return o1.icon.compareTo(o2.icon);
            } if(field.equals("title")) {
                return o1.title.compareTo(o2.title);
            } else if(field.equals("message")) {
                return o1.message.compareTo(o2.message);
            } 
            .
            . fill in the rest of the fields...
            .
            else if(field.equals("locSeen")) {
                return o1.locSeen.compareTo(o2.locSeen);
            } 
        }           
    });
}
+14
source

See JavaDocs for java.util.Comparableand java.util.Comparator.

, Comparable, . . , , Comparator. A Comparator - , , , .

, , Comparator , , , .

Comparable Comparator : 0, 0 0, , . Comparable this.

+5

:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * ComparableDemo
 * @author Michael
 * @since 2/24/11
 */
public class ComparableDemo
{
    public static void main(String[] args)
    {
        List<ItemLocation> itemLocations = new ArrayList<ItemLocation>();
        for (String arg : args)
        {
            itemLocations.add(new ItemLocation(arg));
        }

        System.out.println("before sort: " + itemLocations);
        Comparator<ItemLocation> comparator = new ItemLocationComparator();
        Collections.sort(itemLocations, comparator);
        System.out.println("after  sort: " + itemLocations);
    }
}

class ItemLocation
{
    String icon;
    String title;
    String message;
    String subtext;
    String deviceId;
    double latCoords;
    double lngCoords;
    int expiary;
    int id;
    double proximity;
    String locSeen;

    ItemLocation(String message)
    {
        this("", "", message, "", "", 0.0, 0.0, 0, 0, 0.0, "");
    }

    ItemLocation(String icon, String title, String message, String subtext, String deviceId, double latCoords, double lngCoords, int expiary, int id, double proximity, String locSeen)
    {
        this.icon = icon;
        this.title = title;
        this.message = message;
        this.subtext = subtext;
        this.deviceId = deviceId;
        this.latCoords = latCoords;
        this.lngCoords = lngCoords;
        this.expiary = expiary;
        this.id = id;
        this.proximity = proximity;
        this.locSeen = locSeen;
    }

    @Override
    public String toString()
    {
        final StringBuilder sb = new StringBuilder();
        sb.append("ItemLocation");
        sb.append("{message='").append(message).append('\'');
        sb.append('}');
        return sb.toString();
    }
}



class ItemLocationComparator implements Comparator<ItemLocation>
{
    public int compare(ItemLocation o1, ItemLocation o2)
    {
        return o1.message.compareTo(o2.message);
    }
}

:

C:\JDKs\jdk1.6.0_21\bin\java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 10.0.2\bin" -Dfile.encoding=windows-1252 com.intellij.rt.execution.application.AppMain ComparableDemo zeb meme apple
before sort: [ItemLocation{message='zeb'}, ItemLocation{message='meme'}, ItemLocation{message='apple'}]
after  sort: [ItemLocation{message='apple'}, ItemLocation{message='meme'}, ItemLocation{message='zeb'}]

Process finished with exit code 0
+5
source

Say we have a class with an int and a string. I can determine how one object of this class can be compared with another.

I could choose any criteria. For example, I can decide to sort based on int. If I have two ints with the same value, I can select the line as an additional criterion, something like this:

 // this class *knows* how  to  "compare" against him self
class CustomObject implements Comparable<CustomObject> { 
    String aString;
    int aInt;
     ...
    public int compareTo(CustomObject two ) {
        int diff = this.aInt - two.aInt;//<-- compare ints
        if( diff != 0 ) { // they have different int
            return diff;
        }    
        return this.aString.compareTo( two.aString );//<-- compare strings... 
   }
   ...
}

Here is the full working demo ...

import java.util.*;
class SortDemo { 
    public static void main( String ... args ) { 
        // create a bunch and sort them 
        List<CustomObject> list = Arrays.asList(
            new CustomObject(3, "Blah"),
            new CustomObject(30, "Bar"),
            new CustomObject(1, "Zzz"),
            new CustomObject(1, "Aaa")
        );
        System.out.println( "before: "+ list );
        Collections.sort( list );
        System.out.println( "after : "+ list );
    }
}
 // this class *knows* how  to  "compare" against him self
class CustomObject implements Comparable<CustomObject> { 
    String aString;
    int aInt;
    CustomObject( int i, String s ) { 
        aInt = i;
        aString = s;
    }
    // comparable interface lets you
    // specify "HOW" to compare two 
    // custom objects
    public int compareTo(CustomObject two ) {
        // I migth compare them using the int first 
        // and if they're the same, use the string... 
        int diff = this.aInt - two.aInt;
        if( diff != 0 ) { // they have different int
            return diff;
        }

        // else let the strings compare them selves
        return this.aString.compareTo( two.aString );
   }
   public String toString(){
       return "CustomObject[aInt="+aInt+", aString="+aString+"]";
   }
}

Here's the conclusion:

before: [CustomObject[aInt=3, aString=Blah], CustomObject[aInt=30, aString=Bar], CustomObject[aInt=1, aString=Zzz], CustomObject[aInt=1, aString=Aaa]]
after : [CustomObject[aInt=1, aString=Aaa], CustomObject[aInt=1, aString=Zzz], CustomObject[aInt=3, aString=Blah], CustomObject[aInt=30, aString=Bar]]

Hope that is clear enough

You can also pass a custom comparator. Let me know if you need a sample of this.

+3
source

Source: https://habr.com/ru/post/1794609/


All Articles