Solved

Sorting arrays

Posted on 2003-11-17
9
543 Views
Last Modified: 2010-03-31
I have an multidimensional array of Strings that I need to sort.  What is the fastest sorting algorithm that I can use to sort based on one column and what is the code?
0
Comment
Question by:sre23
  • 4
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9766523
The one in the API. Collections.sort(List l, Comparator c)
0
 
LVL 1

Author Comment

by:sre23
ID: 9766595
ok, well in that case my question gets a little more complex.  I was hoping to just find a few methods that would do it and adjust them to fit my needs.  What I actually have is a DefaultTableModel with 10 columns and I need to sort this table on alphabetically on the 3rd column.  I looked in the API but I am a little confused on how to create a Comparator and List object.  Can you elaborate on that a little.  I've raised the points to 100 since my question has changed a little.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 9767061
class MyTableModel extends DefaultTableModel implements Comparator {

         int sortColumn;

         public void sortColumn(int sortColumn) {
              this.sortColumn = sortColumn;
              Collections.sort(dataVector,this);
         }

         public int compare(Object o1, Object o2)  {
              if (o1 == null) return -1;
              if (o2 == null) return 1;

              Vector vect1 = (Vector) o1;
              Vector vect2 = (Vector) o2;
              Comparable col1 = (Comparable) vect1.get(sortColumn);
              Comparable col2 = (Comparable) vect2.get(sortColumn);
              return col1.compareTo(col2);
         }

}
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 2

Expert Comment

by:dyanet
ID: 9767185
Here is code that will do it.  You have to worry about making sure table is not updated while sorting, handling class cast and null, if there is a chance of that happening.  For regular alphabetic sorting, you do not need a comparator.  Post if you need a sample comparator.




javax.swing.table.DefaultTableModel table = new javax.swing.table.DefaultTableModel(10,10);

//load data
table.setValueAt("rasldkfj",0,2);
table.setValueAt("wqerpoiu",1,2);
table.setValueAt("nevernul",2,2);
table.setValueAt("aslgfwpo",3,2);
table.setValueAt("zasdflks",4,2);
table.setValueAt("bajklsdf",5,2);
table.setValueAt("oqpwiuir",6,2);
table.setValueAt("Adskfjwo",7,2);
table.setValueAt("Zasdflk9",8,2);
table.setValueAt("8slagjwi",9,2);

//create array
int ln = table.getRowCount();
String[] test = new String[ln];

//load array
for (int i=0;i<ln;i++) {
      if (table.getValueAt(i,2) == null)
            test[i]="";
      else test[i]=(String)table.getValueAt(i,2);
}

java.util.Arrays.sort(test);  // no comparator required for String, built-in alphabetic comparator.
      // can use non-casesensitive comparator etc.

// lets see
for (int i=0;i<test.length; i++) System.out.println(test[i]);

// good, set it

for (int i=0;i<test.length; i++) table.setValueAt(test[i],i,2);
0
 
LVL 1

Author Comment

by:sre23
ID: 9767301
CEHJ, in your example how is the sort started? and what is dataVector?  And I assume that "this" can be replaced by the name of my DefaultTableModel?
0
 
LVL 1

Author Comment

by:sre23
ID: 9767321
dyanet, your example looks as though it just sorts the 3rd column.  I need to sort my entire table ON the 3rd column.  I can't just reorder one column otherwise the whole table will be changed.  Each row needs to stay intact.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9767357
dataVector is a protected field of DTM:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/DefaultTableModel.html#dataVector

>>how is the sort started?

By calling sortColumn on the model. Just merge the code with your model
0
 
LVL 1

Author Comment

by:sre23
ID: 9774312
Thanks again CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9774827
:-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
more than one jdk and one jre 1 42
check java version using powershell 13 95
eclipse console opening separately 2 21
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question