Reputation: 21
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]);
}
}
Upvotes: 2
Views: 707
Reputation: 420991
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.
Upvotes: 4
Reputation: 18334
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.
Upvotes: 0
Reputation: 4578
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.
Upvotes: 0
Reputation: 4543
you can use TreeMap and print its content with iterator for keys. You may have to implement your own Comparator.
Upvotes: 1
Reputation: 346310
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.
Upvotes: 7
Reputation: 8526
rather than give you the code, I would point you on the following path: TreeMap. Read, learn, implement
Upvotes: 0