简体   繁体   中英

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. But when the words have 3 combinations it doesn't come out alphabetically.

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?

For example (tried it on 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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