简体   繁体   English

如何按字母顺序对我的输出进行排序

[英]how to sort my output alphabetically java

I'm trying to get read 2 txt files one jumbled and one dictionary file to match the ones with same letters. 我正在尝试读取2个txt文件,其中一个混杂,一个字典文件,以匹配具有相同字母的文件。 But when the words have 3 combinations it doesn't come out alphabetically. 但是,当单词有3个组合时,就不会按字母顺序出现。

What could i do to fix this? 我该怎么做才能解决此问题?

import java.io.*;
import java.util.*;

public class Project6
{

    public static void main( String args[] ) throws Exception
    {
        if (args.length<2)
        {System.out.println( "Must pass name of 2 input files on cmd line. (dictionary.txt, jumbles.txt)");
            System.exit(0);
        }
        long startTime = System.currentTimeMillis();  // like clicking START on your stopwatch

        int maxDictWords = 180000;   //has fixed length data structure... was not that familiar with array list and this is faster.

        int maxJumbleWords = 100;         

        BufferedReader dictFile = new BufferedReader( new FileReader(args[0]) ); 

        BufferedReader jumblesFile = new BufferedReader( new FileReader(args[1]) );


        String[] dictionary = new String[maxDictWords]; // save dictionary txt into an array

        String[] jumbles = new String[maxJumbleWords];// save jumblestxt into an array

        String [] sortDict = new String [maxDictWords]; //into a sort dictionary

        String [] sortJumbles = new String [maxJumbleWords]; // into sort jumbles


        int dWordCnt=0, jWordCnt=0;

        Arrays.sort(dictionary, 0, dWordCnt);
        // For file reading 


        while ( dictFile.ready() )   
        {
            String dictionaryWord = dictFile.readLine();  // grab the whole line because the whole line is just one word

            dictionary[dWordCnt] = dictionaryWord;

            String scrambleWord =  toCanonical(dictionaryWord);

            sortDict [dWordCnt] = scrambleWord; 

            dWordCnt++;
        }

        dictFile.close();

         while ( jumblesFile.ready() )   // same as above
        {
            String jumblesWord = jumblesFile.readLine();  
            jumbles[jWordCnt] = jumblesWord;

            String scrambleWord = toCanonical(jumblesWord);

            sortJumbles [jWordCnt] = scrambleWord;

            jWordCnt++;
        }

        jumblesFile.close();

        String [] result = new String [maxJumbleWords];
        for(int k= 0; k < jWordCnt; k++){

             { result[k]= jumbles[k] + " " ;
            for (int j = 0; j < dWordCnt; j++)
            {
                if (sortJumbles[k].equals(sortDict[j]))
                {

                result[k]= result[k] + dictionary[j] + " ";
                } 
          }
         }
         }

        Arrays.sort(result,0, jWordCnt);
        for(int k = 0; k < jWordCnt; k++){

            System.out.println(result[k]);
        }
         long endTime = System.currentTimeMillis();  // like clicking STOP on your stopwatch
        long ms = endTime-startTime;
        System.out.println("Elapsed time in seconds: " + ms/1000.0 + "\n"); // 1 ms is a 1,000th of a second

    } // END MAIN

    // Methods


    // to sort the letter into a canonical form 
    private static String toCanonical(String word )
    {
       char[]Arr = word.toCharArray();
        Arrays.sort(Arr);
        String sorted = new String(Arr);
        return sorted;          
    }


} // END Project 6

If you have a choice, why not use it in a Collection to do the famous "Lexicographical" sorting using sorted() method? 如果可以选择,为什么不在Collection中使用它通过sorted()方法sorted()著名的“词典”排序呢?

For example (tried it on IDEONE): 例如(在IDEONE上尝试过):

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> g = new ArrayList<String>();
        g.add("glove");
        g.add("golve");
        g.add("gvloe");
        g.add("gevlo");

        // Before sorting (Natural order)
        for(String s: g){

            System.out.println(s);
        }

        System.out.println("\n----------");
        Collections.sort(g);

        // After sorting (Natural order)
        for(String s: g){

            System.out.println(s);
        }
    }

    System.out.println("----");
    // Convert it to array
    String[] p = g.toArray(new String[g.size()]);

    // Check if the array has the correct sort order?

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

in action 在行动

Does this work for you? 这对您有用吗? NB some code above will be redundant as you can just do the sorting and then convert it to the array (which will have the correct sort order anyway). 注意上面的一些代码将是多余的,因为您可以进行排序,然后将其转换为数组(无论如何它将具有正确的排序顺序)。 I just showed it to help you understand what solution you may prefer. 我只是展示了它,以帮助您了解您可能更喜欢的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM