如何在字符串arraylist中进行迭代排序?

时间:2016-05-20 06:57:35

标签: java sorting arraylist

我有一个带有以下字符串元素的arraylist - >

['STM-1000-H', 'STM-2000-E', 'STM-4000-H', 'STM-200-H', 'SFP-1000-A', 
'SFP-300-H', 'SAFPX-1000-H', 'SAFPX-2000-A', 'STM-1000-H-L1', 'STM-1000-H-L1+VA+GH', 'STM-1000-H-L2']

我想要一个像...一样分组的arraylist。

显示STM- 400, 500, 600, 100, 2000, 4000 (in that order), followed by SPX- 1000, 2000, 4000(按此顺序,然后是SAFPX-1000。此外,我必须按L, L1, L2, M, M1, M2, M3, H, H! and VH(按该顺序)列出每个。

请你协助,Array列表只是字符串元素。

3 个答案:

答案 0 :(得分:0)

您应该创建一个自定义比较器,然后使用它来对arraylist进行排序。

例如(这会执行您要求的一些排序,但不会检查边缘情况,这取决于您要修复)。注意:使用Java 8

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("STM-1000-H");
    list.add("STM-2000-E");
    list.add("SAFPX-1000-H-L2");
    list.add("SAFPX-1000-H-L1");
    list.add("STM-1000-H-L1");
    list.add("SFP-1000-B");
    list.add("SFP-1000-A");

    HashMap hm = new HashMap();
    hm.put("STM", 1);
    hm.put("SFP", 2);
    hm.put("SAFPX", 3);

    list = sort(list, hm);

    for (String s : list) {
        System.out.println(s);
    }
}

public static ArrayList<String> sort(ArrayList<String> list, HashMap<String, Integer> hashmap) {
    Comparator<String> comparator = (string1, string2) ->
    {
        String[] tokens1 = string1.split("-");
        String[] tokens2 = string2.split("-");

        if (hashmap.get(tokens1[0]) > hashmap.get(tokens2[0]))
            return 1;
        else if (hashmap.get(tokens1[0]) < hashmap.get(tokens2[0]))
            return -1;

        if (Integer.parseInt(tokens1[1]) > Integer.parseInt(tokens2[1]))
            return 1;
        else if (Integer.parseInt(tokens1[1]) < Integer.parseInt(tokens2[1]))
            return -1;

        if (tokens1.length > 2 && tokens2.length > 2) {
            int res = tokens1[2].compareTo(tokens2[2]);
            if(res != 0)
            return res;
        }

        if (tokens1.length > 3 && tokens2.length > 3) {
            int res = tokens1[3].compareTo(tokens2[3]);
            if(res != 0)
                return res;
        }

        return 0;
    };

    Collections.sort(list, comparator);
    return list;
}

输出:

STM-1000-H

STM-1000-H-L1

STM-2000-E

SFP-1000-A

SFP-1000-B

SAFPX-1000-H-L1

SAFPX-1000-H-L2

答案 1 :(得分:0)

    Stream.of("STM-1000-H",
            "STM-2000-E",
            "STM-4000-H",
            "STM-200-H",
            "SFP-1000-A",
            "SFP-300-H",
            "SAFPX-1000-H",
            "SAFPX-2000-A",
            "STM-1000-H-L1",
            "STM-1000-H-L1+VA+GH",
            "STM-1000-H-L2")
            .collect(Collectors.groupingBy(s -> s.substring(0, s.indexOf('-'))))
            .entrySet()
            .stream()
            .sorted((e1, e2) -> -e1.getKey().compareTo(e2.getKey()))
            .forEach(e -> {
                System.out.println(e.getKey() + ":");
                e.getValue().stream().sorted().forEach(System.out::println);
            });

-

STM:
STM-1000-H
STM-1000-H-L1
STM-1000-H-L1+VA+GH
STM-1000-H-L2
STM-200-H
STM-2000-E
STM-4000-H
SFP:
SFP-1000-A
SFP-300-H
SAFPX:
SAFPX-1000-H
SAFPX-2000-A

答案 2 :(得分:0)

package com.skadakov.examples.devrev;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 *
 * @author s.kadakov
 */
public class DevRev {

    private final String series;
    private final int model;
    private final String version;
    private final String revision;

    public DevRev(String firmware) {
        String[] ss = firmware.split("-");
        if (ss.length < 3) {
            throw new IllegalArgumentException(firmware);
        }

        this.series = ss[0];
        this.model = Integer.parseInt(ss[1]);
        this.version = ss[2];
        this.revision = ss.length == 3 ? null : ss[3];
    }

    public String getSeries() {
        return series;
    }

    public int getModel() {
        return model;
    }

    public String getVersion() {
        return version;
    }

    public String getRevision() {
        return revision;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + Objects.hashCode(this.series);
        hash = 97 * hash + this.model;
        hash = 97 * hash + Objects.hashCode(this.version);
        hash = 97 * hash + Objects.hashCode(this.revision);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DevRev other = (DevRev) obj;
        if (this.model != other.model) {
            return false;
        }
        if (!Objects.equals(this.series, other.series)) {
            return false;
        }
        if (!Objects.equals(this.version, other.version)) {
            return false;
        }
        return Objects.equals(this.revision, other.revision);
    }

    @Override
    public String toString() {
        return series + "-" + model + "-" + version + (revision != null ? "-" + revision : "");
    }

    public static void main(String[] args) {
        String[] firmare = {
            "STM-1000-H",
            "STM-2000-E",
            "STM-4000-H",
            "STM-200-H",
            "SFP-1000-A",
            "SFP-300-H",
            "SAFPX-1000-H",
            "SAFPX-2000-A",
            "STM-1000-H-L1",
            "STM-1000-H-L1+VA+GH",
            "STM-1000-H-L2"};

        Map<String, List<DevRev>> revs = new TreeMap<>();
        for (String f : firmare) {
            DevRev dr = new DevRev(f);
            List<DevRev> sdevs = revs.get(dr.getSeries());
            if (sdevs == null) {
                sdevs = new ArrayList<>();
                revs.put(dr.getSeries(), sdevs);
            }

            sdevs.add(dr);
        }

        for (Map.Entry<String, List<DevRev>> entry : revs.entrySet()) {
            String series = entry.getKey();
            List<DevRev> devices = entry.getValue();
            System.out.println(series);
            devices.sort(new Comparator<DevRev>() {
                @Override
                public int compare(DevRev o1, DevRev o2) {
                    return new Integer(o1.getModel()).compareTo(o2.getModel());
                }
            });

            for (DevRev dr : devices) {
                System.out.println("-> " + dr);
            }
        }
    }
}