0

I have two lists: 1. with words 2. with respective frequency counts

Now I want to sort both of the list in descending order so that index of a word in the first list matches to that of the second list containing frequency counts, respectively.

Adding a function:

    public String[] process() throws Exception
   {

    String[] ret = new String[20];
    int c=0;

    BufferedReader br = new BufferedReader(new FileReader(inputFileName));
    String line = br.readLine();
    List<String> result  = new ArrayList<String>();
    List<Integer> sorted = new ArrayList<Integer>();
    List<String> key= new ArrayList<String>();
    List<String> new_list = new ArrayList<String>();
    int x=0;
    while(line!=null){
        StringTokenizer st = new StringTokenizer(line,delimiters);
        String token = "";
        while (st.hasMoreTokens()) {
            token = st.nextToken();
            //System.out.println(token);
            if(token!=null)
            {
            //System.out.println(token);
                result.add( x,token.toLowerCase());

            //System.out.println("Key is" + x + "\t" + result.get(x));
                x++;
            }
        }
        line=br.readLine();
    }
            for(int w =0;w<x;w++){
        c=0;
        String copy=result.get(w);
        int i;
        for(i =0;i<stopWordsArray.length;i++){
            if(copy.compareTo(stopWordsArray[i])==0){
      c=1;
      break;
      }
      }
     if(c==0){
      new_list.add(copy);
      }
      }
      if(c==0){

    Map<String, Integer> map = new HashMap<String, Integer>();
    for (String temp : new_list) {
    Integer count = map.get(temp);
    map.put(temp, (count == null) ? 1 : count + 1);
   }

int i=0;
int  sort = 0;
String key1 = "";
for (Map.Entry<String, Integer> entry : map.entrySet()) {
 sort = entry.getValue();
 key1 = entry.getKey();
 sorted.add(i,sort);
 key.add(i,key1);

 i++;
  }
   Integer maxi= Collections.max(sorted);
   System.out.println(maxi);
   Integer value = sorted.indexOf(maxi);
   System.out.println(value);
   System.out.println("Word is:"  + key.get(value));

   }
  return ret; }

Here sorted is a list which contains frequencies of words and key is list which contains word.

App Work
  • 21,899
  • 5
  • 25
  • 38
  • 7
    What do you mean by "in front of"? Are you trying to keep two lists of parallel data? If so, the proper solution is to keep one list of a composite data type, rather than trying to manage two lists of 'associated' data. – azurefrog Sep 09 '15 at 21:20

3 Answers3

2

One option is to create a class with two members: word and frequency. Create a Comparator or implement Comparable to sort based on the frequency, then implement toString() to print it however you like.

Bobulous
  • 12,967
  • 4
  • 37
  • 68
1

I don't completely understand the situation, but throwing this out there.

You could use a Map<String,Integer> to store your data with the mapping Word -> Frequency. Now if you use TreeMap it automatically sorts according to the keys (words in your case). Now if you want to sort by values (frequency) , follow this SOF Post - TreeMap sort by value

Community
  • 1
  • 1
afrin216
  • 2,295
  • 1
  • 14
  • 17
0

Map will not work if there are duplicate words. The last value of the same key will replace the earlier. So the solution that comes to my mind is as follows:

SortingWordAndCounts.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class SortingWordAndCounts {

    public static void main(String args[]) {
        ArrayList<WordFreq> wordFreqList = new ArrayList<WordFreq>();

        for (int i = 10; i >= 0; i--) {
            WordFreq wf = new WordFreq();
            wf.setWord("Word" + (i + 1));
            wf.setFrequency(i + 10);
            wordFreqList.add(wf);
        }

        System.out.println("===== Unsorted Result=====");

        for (WordFreq wf : wordFreqList) {
            System.out.println(wf.word + "=" + wf.frequency);
        }

        System.out.println("===== sort by Word=====");
        // Now Sort list and print
        for (WordFreq wf : new SortingWordSAndCounts().sortByWord(wordFreqList,"DESC")) {

            System.out.println(wf.word + "=" + wf.frequency);

        }
System.out.println("===== sort by Frequency=====");

        // Now Sort list and print
        for (WordFreq wf : new SortingWordSAndCounts().sortByFrequency(wordFreqList,"DESC")) {

            System.out.println(wf.word + "=" + wf.frequency);

        }


    }

    public ArrayList<WordFreq> sortByWord(ArrayList<WordFreq> wordFreqList, String sortOrder) {
        Comparator<WordFreq> comparator = new Comparator<WordFreq>() {

            @Override
            public int compare(WordFreq o1, WordFreq o2) {

                if (sortOrder.equalsIgnoreCase("DESC"))
                    return o2.word.compareTo(o1.word);
                else
                    return o1.word.compareTo(o2.word);

            }
        };

        Collections.sort(wordFreqList, comparator);
        return wordFreqList;
    }

    public ArrayList<WordFreq> sortByFrequency(ArrayList<WordFreq> wordFreqList, String sortOrder) {
        Comparator<WordFreq> comparator = new Comparator<WordFreq>() {

            @Override
            public int compare(WordFreq o1, WordFreq o2) {

                if (sortOrder.equalsIgnoreCase("DESC"))

                    return o2.frequency - o1.frequency;
                else
                    return o1.frequency - o2.frequency;

            }
        };

        Collections.sort(wordFreqList, comparator);
        return wordFreqList;
    }

}

Create the pojo:

WordFreq.java


public class WordFreq {

    String word;
    int frequency;
    public String getWord() {
        return word;
    }
    public void setWord(String word) {
        this.word = word;
    }
    public int getFrequency() {
        return frequency;
    }
    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

}

Hope it helps.

App Work
  • 21,899
  • 5
  • 25
  • 38