对自定义对象的矢量进行排序

Kev*_*ker 3 java sorting vector

如何对自定义对象的矢量进行排序并选择要排序的属性?

我确实看到了这个问题和答案,但我不太清楚它的排序是基于什么的.代码示例将优先于"方法论".

对自定义对象的矢量进行排序

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;
}
Run Code Online (Sandbox Code Playgroud)

Kur*_*lor 15

下面是一个允许您按ItemLocation的指定字段排序的示例:

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);
            } 
        }           
    });
}
Run Code Online (Sandbox Code Playgroud)


Bry*_*yle 5

请参阅JavaDocs for java.util.Comparablejava.util.Comparator.

Comparable可以将实现的类与该类的其他实例进行比较.这对于实现自然搜索顺序很有用.要允许除了类的自然顺序之外的其他顺序,您需要实现一个Comparator.A Comparator是一个单独的对象,能够使用它想要的任何标准来比较两个其他对象.

在您的情况下,您可能希望为Comparator要排序的每个不同属性实现a ,或者可以配置一个.

Comparable并且Comparator两者都使用相同的想法来确定排序:方法返回小于0,0或大于0的方法,以通知调用者首先排序2个对象中的哪个.在Comparable第一个对象的情况下this.


duf*_*ymo 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

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
Run Code Online (Sandbox Code Playgroud)