【问题标题】:Need help sorting a textfile alphabetically需要帮助按字母顺序对文本文件进行排序
【发布时间】:2015-04-08 20:44:07
【问题描述】:

好的,所以我需要列出未排序的单词:

freddy
at
elephant
whoooooodat
alice
tommy
bobby
it
at
about

并按长度和字母顺序对其进行排序:

at
at
it
about
alice
bobby
tommy
freddy
elephant
whoooooodat

我可以用 Collections.sort(words) 对其进行排序并得到:

at
it
at
alice
tommy
bobby
about
freddy
elephant
whoooooodat

我只需要按字母顺序排列单词。

这是我当前的代码:

public class Lab1
{
    public static void main( String args[] ) throws IOException
    {
        Scanner file = new Scanner(new File("lab1.dat"));

        ArrayList<Word> words = new ArrayList<Word>();
        while(file.hasNext())
        {
            //add in a new Word to words
            words.add(new Word(file.next()));
        }

        //can sort the words with this 
        Collections.sort(words);


        //make letter lower case and compare asii values
        // need to sort the words by alphabetically 
        /*
        for (int i = 0; i < words.size()-1 ; i++)
        {
            int min = i; 
            for(int j= i+1; j < words.size(); j++){
                String minstring = (String)words.get(min);
            if((((String)words.get(min)).toLowerCase()).compareTo(words.get(j)) > 0)
                min =j;



            }// end of inner loop

            if (min!= i){
            Word temp = words.get(min); 
            words.set(min,words.get(i));
            words.set(i,temp);
            }// end of sort if

        }   //end of outer loop
*/
        //print out words
        for (int j =0; j < words.size(); j++)
        System.out.println(words.get(j));
    }
}

 //Second Part
public class Word implements Comparable<Word>
{
    private String word;

    //constructors

    public Word()
    {
        word = "";
    }
    public Word(String a)
    {
        word =a;
    }



    //compareTo

    public int compareTo(Word other)
    {
        /*if (word.equals(other))
        {   
            if((word.charAt(0)).toLowerCase() )

        }*/
        return word.length() - other.word.length();
    }


    public String toString()
    {
        return word;

    }

}

如果你能帮助我,那就太好了……

【问题讨论】:

  • 您的 compareTo(...) 方法比较值的长度,这意味着您的列表将按大小排序,而不是按字母顺序。

标签: java arrays sorting arraylist


【解决方案1】:
public class MyComparator implements Comparator<String>{
    @Override
    public int compare(String o1, String o2) {  
      if (o1.length() > o2.length()) {
         return 1;
      } else if (o1.length() < o2.length()) {
         return -1;
      }
      return o1.compareTo(o2);
    }
}

然后:

Collections.sort(yourList, new MyComparator());

【讨论】:

    【解决方案2】:

    您的比较方法也应该实现字母顺序:

    public int compareTo(Word other)
        {
            int difference = word.length() - other.word.length();
            if(difference == 0){
                return word.compareToIgnoreCase(other.word);
            else{
                return difference;    
            }
    
        }
    

    【讨论】:

    • 谢谢!效果很好,一开始就没有真正理解过。
    • compareTo 覆盖 Comparable 中的方法,例如由 Collections.sort 调用以决定哪些对象在另一个之前。您只返回 -1,0,1 对象是否相同或不同。阅读文档以获取更多信息:)
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多