按预定义的自定义顺序排序字符串列表 [英] Sort String list in pre-defined custom ordering

查看:145
本文介绍了按预定义的自定义顺序排序字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个项目,程序会询问用户应该运行多少次。在每次运行期间,将创建一个列表并添加元素。数组中的这些元素按字母顺序排序,但按以下顺序排序:

I have this project in which the program asks the user how many times it should run. During each run a list is created and elements are added. These elements in the array are sorted not alphabetically but sorted in the following order:

q,w,e,r,t,y,u,i,o,p,l,k,j,h,g,f,d,s,a,z,x,c,v,b,n,m

我能够将程序运行到接受输入的程度,但我不知道接下来要做什么...

I am able to run my program up to the point where it accepts input, but I don't know what to do next...

样本输入

2

3
what
are
you

2
sad
set

样本输出

what
you
are

set
sad

这是我的代码:

import java.util.*;


public class WeirdWritings {

    public static void main(String[] args) {
        int data,word,ctr,i,ii;
        char[] rLetter = {'Q','W','E','R','T','Y','U','I','O','P','L','K','J','H','G','F','D','S','A','Z','X','C','V','B','N','M'};
        String entry;

        Scanner s = new Scanner(System.in);

        data=s.nextInt();
        for (ctr=0;ctr<rLetter.length;ctr++)
            System.out.print(rLetter[ctr]);
        System.out.println();

        for (ii = 0; ii<data; ii++) {
            word=s.nextInt();
            List<String> array_ii = new ArrayList<String>();
            for(i=0; i<word; i++) {
                entry=s.next();
                array_ii.add(entry);
            }
        }

    }
}

添加比较器以完成它

公共类MyComparator实现Comparator {

public class MyComparator implements Comparator {

private int[] charRank;

public MyComparator() {
    char[] charOrder = {'Q','W','E','R','T','Y','U','I','O','P','L','K','J','H','G','F','D','S','A','Z','X','C','V','B','N','M'};
    this.charRank = new int[26];
    for(int i=0; i<25; i++) {
        this.charRank[charToInt(charOrder[i])] = i; 
    }
}

public int compare(String s1, String s2) {
    // returns
    // Positive integer if s1 greater than s2
    // 0 if s1 = s2
    // Negative integer if s1 smaller than s2
    s1 = s1.toUpperCase();
    s2 = s2.toUpperCase();
    int l = Math.min(s1.length(), s2.length());
    int charComp;
    for(int i=0; i<l; i++) {
        charComp = this.charRank[charToInt(s1.charAt(i))] - charRank[charToInt(s2.charAt(i))];
        if(charComp != 0)
            return charComp;
    }
    return s1.length() - s2.length();
}

//works for c between 'A' and 'Z' - upper case letters
private static int charToInt(char c) {
    return c - 65;
}

//works for 0<=i<=25
private static char intToChar(int i) {
    return (char) (i + 65);
}

public static void main(String[] args) {
    int data,word,ctr,i,ii;
    char[] rLetter = {'Q','W','E','R','T','Y','U','I','O','P','L','K','J','H','G','F','D','S','A','Z','X','C','V','B','N','M'};
    String entry;

    Scanner s = new Scanner(System.in);

    data=s.nextInt();
    for (ctr=0;ctr<rLetter.length;ctr++)
        System.out.print(rLetter[ctr]);
    System.out.println();

    for (ii = 0; ii<data; ii++) {
        word=s.nextInt();
        String[] array_ii = new String[word];
        for(i=0; i<word; i++) {
            entry=s.next();
            array_ii[i]=(entry);
        }
        Arrays.sort(array_ii, new MyComparator());
        for(i=0; i<word; i++) {
            System.out.println(array_ii[i]);
        }
    }



}

}

推荐答案

您可以建立自己的比较器:

You can build your own Comparator:

import java.util.Comparator;

public class MyComparator implements Comparator<String> {

    private int[] charRank;

    public MyComparator() {
        char[] charOrder = {'Q','W','E','R','T','Y','U','I','O','P','L','K','J','H','G','F','D','S','A','Z','X','C','V','B','N','M'};
        this.charRank = new int[26];
        for(int i=0; i<25; i++) {
            this.charRank[charToInt(charOrder[i])] = i; 
        }
    }

    public int compare(String s1, String s2) {
        // returns
        // Positive integer if s1 greater than s2
        // 0 if s1 = s2
        // Negative integer if s1 smaller than s2
        s1 = s1.toUpperCase();
        s2 = s2.toUpperCase();
        int l = Math.min(s1.length(), s2.length());
        int charComp;
        for(int i=0; i<l; i++) {
            charComp = this.charRank[charToInt(s1.charAt(i))] - charRank[charToInt(s2.charAt(i))];
            if(charComp != 0)
                return charComp;
        }
        return s1.length() - s2.length();
    }

    //works for c between 'A' and 'Z' - upper case letters
    private static int charToInt(char c) {
        return c - 65;
    }

    //works for 0<=i<=25
    private static char intToChar(int i) {
        return (char) (i + 65);
    }

}

然后,你只需要运行:

Arrays.sort(entryArray, new MyComparator());

现在有一些解释。

构造函数MyComparator为每个字母构建一系列排名。在实践中,它将以 {18,23,21,...} 开头,因为'A'位于第18位,'B'位于第23位...

The constructor of MyComparator builds an array of ranks for each of your letter. In practice it will begin with {18, 23, 21, ... } because 'A' is in the 18th place, 'B' in the 23rd...

然后,比较两个字符串 s1 s2 ,字符在字典顺序中逐一进行比较。

Then, when you compare two strings s1 and s2, characters are compared in the lexicographic order one by one.


  • 想象一下第一个 n 字符相同,现在,您比较 c1 c2 。如果 c1 的排名低于 c2 的排名,则必须返回一个负整数,因为 s1 在字典顺序中 s2 之前( s1 小于 S2 )。如果 c1 的排名大于 c2 的排名,则相反。如果两个等级都等于 c1 并且 c2 等于,则必须查看下一个字符。

  • 如果到达一个字符串的末尾,则两个字符串都是等号,或者一个字符串是另一个字符串的前缀。在每种情况下,足以比较字符串的长度:如果 s1 更短,它在字典顺序中排在第一位,它更小,你必须返回一个负整数。

  • Imagine the first n characters are identical, and now, you compare c1 and c2. If the rank of c1 is lower than then rank of c2, then you have to return a negative integer since s1 comes before s2 in the lexicographic order (s1 is smaller than s2). If the rank of c1 is greater than the rank of c2, it is the contrary. If both ranks are equals c1 and c2 are equals and you have to look at the next character.
  • If you reach the end of one string, then either the two string are equals, either one string is the prefix of another. In each case, it is enough to compare the lengths of the strings: if s1 is shorter, it comes first in the lexicographic order, it is smaller, you have to return a negative integer.

在这里你走了。我希望这是你在等待的。

And here you go. I hope it was what you were waiting for.

@Edit: s1 = s1.toUpperCase()是只是为了避免区分大小写字母的痛苦。如果您更喜欢使用小写字母,只需将其更改为 s1 = s1.toLowerCase(),更改方法 intToChar 返回c-97 (小写'a'的ascii代码)自然地,指定 charOrder 构造函数中的数组使用小写字母。

@ the s1 = s1.toUpperCase() is just to avoid the pain to distinguish upper and lower case letters. If you prefer to work with lower case letters, just change it to s1 = s1.toLowerCase(), change the method intToChar to return c-97 (ascii code for a lower case 'a') and naturally, specify the charOrder array in the constructor using lower case letters.

这篇关于按预定义的自定义顺序排序字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆