java—如何对名称的字符串列表使用二级字母排序?

jogvjijk  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(346)

我在尝试一种方法,当给一个名字串,名字是名字和姓氏,其中的名字是分开的 ; 名字和姓氏按 : ->

"Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"

我想返回一个字符串,它将所有的名字按大写字母排序,并根据姓氏和共享相同姓氏的名字按字母顺序排序,然后在共享相同姓氏的人的名字之间再次排序(二次排序)(我还特意将所有内容改为大写)。
并返回排序名称的字符串,因此对于上面的示例,它应该返回:

"(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"

根据姓氏的字母顺序进行排序,然后在拥有相同姓氏的人之间进行排序,第二种是名字。
我设法做了主要的排序,也就是按字母顺序排列姓氏,但我不知道如何做第二排序(在那些有相同姓氏的人之间)。我在考虑拆分成子数组或类似的东西,但我不知道怎么做。
解决这个问题的办法是什么?
以下是我迄今为止所做的(主要排序工作):

public class Solution {
    public static String meeting(String s) {
        String m = "";
        List<String> name = new ArrayList<String>();
        for (String i : s.toUpperCase().split(";")) {
            String[] n = i.split(":");
            name.add(n[0] + " " + n[1]);
        }
        //main sorting
        java.util.Collections.sort(name);
        //secondary sorting and go through each different last name
        for (String i : name) {
            String[] n = i.split(" ");
            String lastname = n[1];
            // new list for the ppl who share same last name
            List<String> samel = new ArrayList<String>();
            samel.add(n[0]);
            for (String ss : name) {
                String[] nn = ss.split(" ");
                if (nn[1] == lastname) {
                    samel.add(nn[0]);
                }
            }
            //sort the ppl alphabetically with the same last name
            java.util.Collections.sort(samel);
        }
        for (String i : name) {
            String[] n = i.split(" ");
            m += ("(" + n[0] + " , " + n[1] + ")");
        }
        return m;
    }
}

我尝试进行二次排序,但没有成功。
如果我不够清楚的话,有两种排序方式,一种是按姓氏字母顺序排序,另一种是按姓氏字母顺序排序,这种排序方式只适用于姓氏完全相同的人,然后他们的名字按字母顺序进行排序。所以,如果我们有两个叫matt cohn和mill cohn的人,那么他们有相同的姓氏和姓氏的第一个字母,但是a在o之前,所以输出应该是 (COHN , MATT)(COHN, MILL) 而不是相反。
在下面的解决方案中,我运行它并得到一个错误:

input String:
Alexis:Wahl;John:Bell;Victoria:Schwarz;Abba:Dorny;Grace:Meta;Ann:Arno;Madison:STAN;Alex:Cornwell;Lewis:Kern;Megan:Stan;Alex:Korn

Actual: (ARNO, ANN)(BELL, JOHN)(CORNWELL, ALEX)(DORNY, ABBA)(KERN, LEWIS)(KORN, ALEX)(META, GRACE)(STAN, MADISON)(SCHWARZ, VICTORIA)(STAN, MEGAN)(WAHL, ALEXIS)

Expect: (ARNO, ANN)(BELL, JOHN)(CORNWELL, ALEX)(DORNY, ABBA)(KERN, LEWIS)(KORN, ALEX)(META, GRACE)(SCHWARZ, VICTORIA)(STAN, MADISON)(STAN, MEGAN)(WAHL, ALEXIS)
false

但结果却是假的

wfauudbj

wfauudbj1#

一种可能的解决方案如下:

public class ExtractAndSortNames {

    public static String extractAndSort(String s) {
        return Stream.of(s.split(";"))
                .map(String::toUpperCase)
                .map(ExtractAndSortNames::extractPersonFirstNameAndLastName)
                .sorted(Comparator.comparing(FirstNameAndLastName::getLastName).thenComparing(FirstNameAndLastName::getFirstName))
                .map(FirstNameAndLastName::format)
                .collect(Collectors.joining());
    }

    private static FirstNameAndLastName extractPersonFirstNameAndLastName(String personName) {
        var split = personName.split(":");
        var firstName = split[0];
        var lastName = split[1];
        return new FirstNameAndLastName(firstName, lastName);
    }

    private static class FirstNameAndLastName {
        private final String firstName;
        private final String lastName;

        public FirstNameAndLastName(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public String format() {
            return String.format("(%s, %s)", this.lastName, this.firstName);
        }
    }
}

无流解决方案:

public class ExtractAndSortNames {

    public static String extractAndSort(String s) {
        List<FirstNameAndLastName> toSort = new ArrayList<>();
        for (String s1 : s.split(";")) {
            String toUpperCase = s1.toUpperCase();
            FirstNameAndLastName firstNameAndLastName = extractPersonFirstNameAndLastName(toUpperCase);
            toSort.add(firstNameAndLastName);
        }
        toSort.sort(Comparator.comparing(FirstNameAndLastName::getLastName).thenComparing(FirstNameAndLastName::getFirstName));
        StringBuilder sb = new StringBuilder();
        for (FirstNameAndLastName firstNameAndLastName : toSort) {
            String format = firstNameAndLastName.format();
            sb.append(format);
        }
        return sb.toString();
    }

    private static FirstNameAndLastName extractPersonFirstNameAndLastName(String personName) {
        var split = personName.split(":");
        var firstName = split[0];
        var lastName = split[1];
        return new FirstNameAndLastName(firstName, lastName);
    }

    private static class FirstNameAndLastName {
        private final String firstName;
        private final String lastName;

        public FirstNameAndLastName(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public String format() {
            return String.format("(%s, %s)", this.lastName, this.firstName);
        }
    }
}

和测试:

class ExtractAndSortNamesTest {

    @Test
    void test() {
        var input = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";
        var result = ExtractAndSortNames.extractAndSort(input);

        assertEquals("(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)", result);
    }

    @Test
    void test2() {
        var input = "Alexis:Wahl;John:Bell;Victoria:Schwarz;Abba:Dorny;Grace:Meta;Ann:Arno;Madison:STAN;Alex:Cornwell;Lewis:Kern;Megan:Stan;Alex:Korn";
        var result = ExtractAndSortNames.extractAndSort(input);

        assertEquals("(ARNO, ANN)(BELL, JOHN)(CORNWELL, ALEX)(DORNY, ABBA)(KERN, LEWIS)(KORN, ALEX)(META, GRACE)(SCHWARZ, VICTORIA)(STAN, MADISON)(STAN, MEGAN)(WAHL, ALEXIS)", result);
    }
}

使用具有预览功能的java 15的奖励回合:

public class ExtractAndSortNames {

    public static String extractAndSort(String s) {
        return Stream.of(s.split(";"))
                .map(String::toUpperCase)
                .map(FirstNameAndLastName::from)
                .sorted(Comparator.comparing(FirstNameAndLastName::lastName).thenComparing(FirstNameAndLastName::firstName))
                .map(FirstNameAndLastName::format)
                .collect(Collectors.joining());
    }

    private static record FirstNameAndLastName (String firstName, String lastName) {
        private static FirstNameAndLastName from(String personName) {
            var split = personName.split(":");
            var firstName = split[0];
            var lastName = split[1];

            return new FirstNameAndLastName(firstName, lastName);
        }

        public String format() {
            return "(%s, %s)".formatted(this.lastName, this.firstName);
        }
    }
}
htzpubme

htzpubme2#

可以使用比较器链先按一个值排序,然后按另一个值排序:

String str = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;" +
        "Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";

List<Map.Entry<String, String>> list = Stream.of(str)
        .map(String::toUpperCase)
        // Stream<String>
        .flatMap(s -> Arrays.stream(s.split(";")))
        // Stream<String[]>
        .map(s -> s.split(":"))
        // Stream<Map.Entry<String,String>>
        .map(arr -> Map.entry(arr[1], arr[0]))
        // sort by last name, then by first name
        .sorted(Map.Entry.<String, String>comparingByKey()
                .thenComparing(Map.Entry::getValue))
        .collect(Collectors.toList());

// output line by line
list.forEach(System.out::println);

输出:

CORWILL=ALFRED
CORWILL=FRED
CORWILL=RAPHAEL
CORWILL=WILFRED
TORNBULL=BARNEY
TORNBULL=BETTY
TORNBULL=BJON

相关问题