tags:

views:

93

answers:

6

hi friends,

I have text file with list of alphabets and numbers. I want to do sorting w.r.t this number using java.

My text file looks like this:

a--->12347
g--->65784
r--->675

I read the text file and i split it now. But i dont know how to perform sorting . I am new to java. Please give me a idea.

My output want to be

g--->65784
a--->12347
r--->675

Please help me. Thanks in advance.

My coding is

String str = "";
BufferedReader br = new BufferedReader(new FileReader("counts.txt"));
while ((str = br.readLine()) != null) {
String[] get = str.split("---->>");

When i search the internet all suggest in the type of arrays. I tried. But no use.How to include the get[1] into array.

    int arr[]=new int[50]
    arr[i]=get[1];
    for(int i=0;i<50000;i++){
                for(int j=i+1;j<60000;j++){
                   if(arr[i]>arr[j]){
                       System.out.println(arr[i]);
                   }
                }
A: 

rather than give you the code, I would point you on the following path: TreeMap. Read, learn, implement

Ryan Fernandes
+6  A: 

You should use the Arrays.sort() or Collections.sort() methods that allows you to specify a custom Comparator, and implement such a Comparator to determine how the strings should be compared for the purpose of sorting (since you don't want the default lexicographic order). It looks like that should involve parsing them as integers.

Michael Borgwardt
+1  A: 

you can use TreeMap and print its content with iterator for keys. You may have to implement your own Comparator.

binary_runner
A: 

What you want to do is:

1) convert the numbers into integers 2) Store them in a collection 3) use Collections.sort() to sort the list.

Chad Okere
+4  A: 

Your str.split looks good to me. Use Integer.parseInt to get an int out of the string portion representing the number. Then put the "labels" and numbers in a TreeMap as described below. The TreeMap will keep the entries sorted according to the keys (the numbers in your case).

import java.util.TreeMap;
public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
        tm.put(12347, "a");
        tm.put(65784, "g");
        tm.put(675,   "r");

        for (Integer num : tm.keySet())
            System.out.println(tm.get(num) + "--->" + num);
    }
}

Output:

r--->675
a--->12347
g--->65784

From the API for TreeMap:

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

aioobe
+1 for giving out an example code. While people are against this, it is still very suitable for new programmers to see and use the coding pattern. || As a footnote, you should just use `import java.util.TreeMap;` as opposed to a wildcard, `*`.
Xavier Ho
Updated. (pad).
aioobe
A: 

I assume that you are an absolute beginner.

You are correct till the split part. You need to place the split number immediately into a string or object (custom object)

You would create something like:

class MyClass //please, a better name, 
{
   //and better field names, based on your functionality
   int number;
   String string;
}

Note: You have to implement equals and hashCode

After the split (your first snippet), create an object of this class, place get[0] into string and get[1] into number (after converting the string to integer)

You place this object into an TreeMap.

Now you have a sorted list.

I have deliberately not specified the details. Feel free to google for any term/phrase you dont understand. By this way you understand, rather than copy pasting some code.

Nivas