473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sort number in ascending

281 100+
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array [1] from 12, 64, 8, 128 etc.
3 12
4 64
7 8
10 128
...


I just wanna sort and number out them :
1 8
2 12
3 64
....

And my work...(I have tried to use bubbleSort() that I copied from people's ..)

Expand|Select|Wrap|Line Numbers
  1. public int[] bubbleSort(int array[]) {
  2. boolean swappedOnPrevRun = true;
  3. while(swappedOnPrevRun)
  4. {
  5.  swappedOnPrevRun = false;
  6.  for(int i = 0; i < array.length - 1; i++){
  7.  if(array[i] > array[i + 1])
  8. {
  9.  swappedOnPrevRun = true;                  
  10. int temp = array[i];    
  11. array[i] = array[i + 1];    
  12. array[i + 1] = temp;                          }
  13.                   }
  14.               }
  15.  return array;
  16.           }
  17.  
  18.      private static String[] readLines (String fileName, int m) throws IOException
  19.      {
  20.  
  21.          List<String> lines = new ArrayList<String>();
  22.  
  23.          String lineFRST = "";
  24.          Boolean readable = true;
  25.  
  26.         BufferedReader in1 = new BufferedReader (new FileReader( m + "File.txt"));
  27.  
  28.  
  29.          while (readable)
  30.         {
  31.           lineFRST = in1.readLine();
  32.                 if (lineFRST != null)
  33.       {
  34.      lines.add(lineFRST);
  35.          readable = true;
  36.       }
  37.            else
  38.         readable = false;
  39.         }
  40.  
  41.   return lines.toArray(new String[lines.size()]);
  42.  
  43.      }
  44.  
  45.      public static void main(String[] args) throws IOException
  46.     {
  47.  
  48.         PrintWriter printWriter = null;
  49.         BufferedWriter bufferWriter = null;
  50.  
  51.    for (int m=1; m<=40; m++)  
  52.        {
  53.          String[] lines = readLines (m + "Out.txt", m);  //read all lines from your file
  54.  
  55.          printWriter = new PrintWriter(new BufferedWriter(new FileWriter(m +"SORTED.txt", true)));
  56.  
  57.          for(int i = 0; i < lines.length; i++)
  58.          {
  59.             String[] theline = lines[i].split(" ");
  60.  
  61.             /*String[] num = theline[0];
  62.               String[] toSort = theline[1];*/
  63.  
  64.             // need to convert string to int first....               
  65.  
  66.             printWriter.println( (i+1) + " : " + lines[i]);
  67.         }
  68.     \\      bubbleSort ();  \\need to parse the int
  69.  
  70.         printWriter.close();
  71.   }
  72.  
  73.      }
May 27 '07 #1
6 4044
JosAH
11,448 Expert 8TB
Have a look at the article "generic heap sort algorithm" in the Java Articles section.

kind regards,

Jos
May 27 '07 #2
shana07
281 100+
Have a look at the article "generic heap sort algorithm" in the Java Articles section.

kind regards,

Jos
Jos, assist me on array syntax..what's wrong with my syntax here..
I will study about the article..thank you very much
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < lines.length; i++)
  2.          {
  3.             String[] theline = lines[i].split(" ");
  4.  
  5.             String[] testcase = theline[i];
  6.             String[] eod = theline[i];
  7.  
  8.             printWriter.println( (i+1) + " : " + lines[i]);
  9.         }
May 27 '07 #3
JosAH
11,448 Expert 8TB
Jos, assist me on array syntax..what's wrong with my syntax here..
I will study about the article..thank you very much
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < lines.length; i++)
  2.          {
  3.             String[] theline = lines[i].split(" ");
  4.  
  5.             String[] testcase = theline[i];
  6.             String[] eod = theline[i];
  7.  
  8.             printWriter.println( (i+1) + " : " + lines[i]);
  9.         }
"lines" is an array of Strings and so is "theline" but "theline[ i ]" is just one
single String and you can't cast a single String to another String array where
you want to initialize variable "testcase".

kind regards,

Jos
May 27 '07 #4
shana07
281 100+
"lines" is an array of Strings and so is "theline" but "theline[ i ]" is just one
single String and you can't cast a single String to another String array where
you want to initialize variable "testcase".

kind regards,

Jos
The article is about sorting name, please assist me on sorting integer ...
What do you think about the buble sort method as above ?
May 27 '07 #5
shana07
281 100+
The article is about sorting name, please assist me on sorting integer ...
What do you think about the buble sort method as above ?
I'm sorry.I think I almost get there..I use this
Expand|Select|Wrap|Line Numbers
  1. String[] lines = readLines (m + "Input.txt", m);  //read all lines from your file
  2.  printWriter = new PrintWriter(new BufferedWriter(new FileWriter(m +"SORTED.txt", true)));
  3.  Arrays.sort (lines);
  4.  
  5.   for( i = 0; i < lines.length; i++)
  6.    {
  7.         printWriter.println("lines["+(i+1) +"] = "+lines[i]);
  8.  
  9.    }
But it's not yet correct order...please see below example..assist me anyone.
lines[1] = 100
lines[2] = 100
lines[3] = 104
lines[4] = 108
lines[5] = 112
lines[6] = 112
lines[7] = 112
lines[8] = 112
lines[9] = 12
lines[10] = 12
lines[11] = 12
May 27 '07 #6
JosAH
11,448 Expert 8TB
The article is about sorting name, please assist me on sorting integer ...
What do you think about the buble sort method as above ?
Nope, bubble sort is a silly thing and nope, that article isn't about sorting names;
it's about anything that happens to be Sortable. Please read that article again
and try to understand what object oriented programming is all about.
The examples just showed two arrays of Strings that need to be sorted. It could
as easily have sorted two arrays of Integers or whatever; as long as that 'whatever'
implements the Sortable interface.

kind regards,

Jos
May 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
6
by: Les Juby | last post by:
I need to extract records from a database subject to conditions and only thereafter give the users the choice of which fields to sort the results on. In this situation I can't write back to a...
13
by: artev | last post by:
If sort this work: var myarray= new Array(10,16,35,"0.1",8,4,22,19,1,22,35,9,26,38,40); with code function function1(a,b) {return a - b} var order02=new Array();...
7
by: pstachy | last post by:
Hi all, I'm having problem with XSLT transformation concerned with sort and numberings at a time. I wish to have my registries sorted alfabetically and would like them to have numberings at a...
15
by: bcochofel | last post by:
Hi, I want to use a variable to sort elements. That var his passed with query string (I'm using Perl CGI to generate XML). Here's a sample of my output:...
5
by: reon | last post by:
Di anyone pls help me out ...when i entered 10 numbers.. i want to group that numbers in to ascending orders as output .. Did anyone pls help to to dis....
6
by: reon | last post by:
Here is my source code.... And any one pls help me how can we find the output integers sort by ascending and find there average... #include<iostream.h> #include<conio.h> void main() { int...
1
by: mktilu | last post by:
hi With ListView1 .SortKey = ColumnHeader.Index - 1 .SortOrder = IIf((.SortOrder = lvwAscending), lvwDescending, lvwAscending) .Sorted = True End With this the...
3
by: Anja Friedrich | last post by:
Hi all, I have another question. I have an array sorted like this: small_motif_a 1853 1863 small_motif_a 1970 1980 small_motif_a 1971 1981 small_motif_b 789 799 small_motif_b 882 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.