473,326 Members | 2,099 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,326 software developers and data experts.

Sorting a String array

Hi,

I'm reading in a delimeted text file (X rows, 4 columns) in this case tab delimited. I would like the array to be sorted in the array.

This is the best I have come up with, but it seems very harsh. Is there no easier way of sorting an array?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. String[][] tempData;
  4. tempData = new String[20][4];
  5.  
  6. [ stuff deleted when using CSV to read in the file ]
  7.  
  8. Arrays.sort(tempData, new Comparator<Object>() {
  9.     public int compare(Object o1, Object o2)
  10.     {
  11.     try
  12.     {
  13.         String[] s1 = (String[]) o1;
  14.         String[] s2 = (String[]) o2;
  15.         if ((s1[0] == null) || (s2[0] == null))
  16.         {
  17.             return 0;
  18.         }
  19.         return s1[0].compareTo(s2[0]);
  20.     }
  21.     catch (ParseException e)
  22.     {
  23.         e.printStackTrace();
  24.         return Integer.MIN_VALUE;
  25.     }
  26. }
  27.  
The null check is because although I have a 20x4 String array defined, there is no gurantee that there will be exactly 20 rows, there might be less.

Thanks.
Feb 27 '08 #1
1 1418
BigDaddyLH
1,216 Expert 1GB
I would use a List<SomeClass>, not a String[20][4], where SomeClass is Comparable.
Feb 27 '08 #2

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

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
4
by: VooDoo | last post by:
Hi, I am getting confused with sorting arrays... $arraytest =array(5) { =string(2) "39" =string(2) "44" => string(2) "77" =string(3) "150" =string(3) "464" } why do i get NULL value if i try to...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.