Sorting array of jtables

Hi all
I have a JFrame with 5 JTables on it. I would like to order it as follows:
JTable where cell(0,0) has the least value must be first on the JFrame.
JTable where cell(0,0) has the second least value must be second.
till
JTable where cell(0,0) has the most value must be last on the JFrame.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
public class TestTable extends JFrame{
JTable[] table = new JTable[5];
TableModel[] model = new TableModel[5];
JPanel pane = new JPanel();
int m=0;
public TestTable(){
super();
for(int k=0;k<5;k++){
model[k] = new AbstractTableModel(){
public boolean isCellEditable(int row, int col){
     return false;
public int getColumnCount () {
return 2;
public int getRowCount () {
return 2;
public Object getValueAt (int row, int col) {
m=m+2;
return new Integer ((row*col)+m);
table[k] = new JTable (model[k]);
table[k].setColumnSelectionAllowed(false);
table[k].setRowSelectionAllowed(false);
table[k].setEnabled(false);
pane.add(table[k]);
m=m+2;
setContentPane(pane);
public static void main(String[] args){
TestTable f = new TestTable();
f.pack();
f.setBounds(new Rectangle(20,20,200,300));
f.setVisible(true);
Thanks
Chris

Hi,
have a look at Arrays.sort(Object[] a)
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html

Similar Messages

  • Removes duplicates from a sorted array without having to create a new array

    Funcation removes duplicates from a sorted array without having to create a new array

    Funcation removes duplicates from a sorted array
    without having to create a new arrayIs a 'funcation' like a vacation but more fun or
    something?LMFAO. You just jumped with both feet on one of my biggest peeves. I've been outed!
    Actual words/phrases I've seen used which I hate with every fiber of my being:
    "Mrs. Freshly's"
    "McFlurry"
    "Freshissimo"
    "Spee-dee"
    "Moons Over My Hammy"
    One of my favorite SNL skits had Will Ferrell leading a panel of movie reviewers (they're the worst for this kind of junk). Each one had some cheesy pun to describe their respective movie. Ferrell topped the show off with his endorsement of "Fantasia 2000 is Fantasgreat!"
    LOL.
    "Come to Slippy Village - it's a FUNCATION!"
    &#167; (runs off, laughing maniacally, head explodes)

  • Exception handling in sorting arrays

    Hi all, I have a problem with the use of the built in java sort fo arrays. My program works on an array with 4 or 5 entities. When I run it in some folders it works but not in others. I get the following error after compilation, where "ResultData" is the class of array to be sorted:Exception in thread "main" java.lang.NullPointerException
    at ResultData.compareTo(TextEdit.java:1215)
    at java.util.Arrays.mergeSort(Arrays.java:1064)
    at java.util.Arrays.mergeSort(Arrays.java:1071)
    at java.util.Arrays.mergeSort(Arrays.java:1072)
    at java.util.Arrays.mergeSort(Arrays.java:1071)
    at java.util.Arrays.mergeSort(Arrays.java:1071)
    at java.util.Arrays.mergeSort(Arrays.java:1071)
    at java.util.Arrays.sort(Arrays.java:1014)
    at java.util.Collections.sort(Collections.java:78)
    [\code]Can any one give a suggestion?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    Here is the code for the class "ResultData"class ResultData implements Comparable
         String file1;
         String file2;
         double var, var1, var2;
         public ResultData(String f1,String f2,double v, double x, double y)
                   file1= f1; file2=f2;
                   var=v; var1=x; var2=y;
         public String getFile1Name()
              return file1;
         public String getFile2Name()
              return file2;
         public double getVar()
              return var;
         public double getVar1()
              return var1;
         public double getVar2()
              return var2;
         public String toString()
              return file1+"          "+file2+"          "+var+"          "+var1+"          "+var2;
           public int compareTo(Object o)
              ResultData r= (ResultData)o;
              double result = this.getVar()- r.getVar();
              if (result < 0.0)         return -1;
              else if(result > 0.0)     return 1;
              else return 0;
    } // ends class ResultData

  • TreeSet vs Collection.Sort / Array.sort for Strings

    Gurus
    I am pondering weather to use TreeSet vs the Collections.sort / Array.sort for sorting Strings.
    Basically I have a list of Strings, i need to perform the following operations on these Strings
    1) Able to list Strings starting with a Prefix
    2) Able to list Strings Lexically greater than a String
    Any help would be greatly appreciated!
    Thanks a bunch

    SpaceShuttle wrote:
    Gurus
    I am pondering weather to use TreeSet vs the Collections.sort / Array.sort for sorting Strings.
    Basically I have a list of Strings, i need to perform the following operations on these Strings
    1) Able to list Strings starting with a Prefix
    2) Able to list Strings Lexically greater than a String
    Any help would be greatly appreciated!
    Thanks a bunchBig-O wise, there's no difference between sorting a list of N elements or inserting them one by one in a tree-set. Both take O(n*log(n)). But both collections are not well suited for looking up string that start with a certain substring. In that case you had better use a Patricia tree (or Radix tree).
    Good luck.

  • Sun C++ std::sort coredump during sorting array of long values

    Hello,
    I got core dump during execution following test program on both Solaris 9 (compile by CC: Sun C++ 5.5 Patch 113817-19 2006/10/13) and Solaris 10 platforms (compile by CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25).
    Core is dumped during sorting array of about 120k elements of unsigned long types using STL std:sort. The input data is available at link: [file.txt|http://www.savefile.com/files/1924004]
    When I change sorting methods to std::stable_sort program works fine.
    The funny thing is that when I change order of last two values of input data (swap), sorting is successful with std::sort.
    Can anyone tell me if it is Sun C++ compiler bug ?Can I use std::stable_sort method safely?
    Below code:
    #include <iostream.h>
    #include <fstream.h>
    #include <set>
    #include <string>
    int main( int argc, char** argv )
      char readStr[256];
      ifstream file;
      unsigned long *l;
      int i=0;
      l = new unsigned long[119016];
      if (l ==0)
        cout << "Error in allocate memory";
        return -1;
      file.open("file.txt");
      if (!file)
        cout << "Error in openening file";
        return -1;
      while(file.getline(readStr, 256,'\n'))
        cout << readStr<<endl;
        l= atol(readStr);
    cout << l[i]<<endl;
    i++;
    std::sort(l,l+119016); //core dump here!!!
    for (i=0;i<119016;i++)
    cout <<l[i]<<endl;
    file.close();
    delete [] l;
    return( 0 );
    Greetings
    Robert                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    You have run into bug 6588033 "std::sort from libCstd loops", which is fixed in current patches.
    Get the current patches for Sun Studio and also the current patch for the C++ runtime libraries (SUNWlibC patch).
    You can find them here:
    [http://developers.sun.com/sunstudio/downloads/patches/index.jsp]
    Sun C++ 5.5 probably does not have the fix, since it is quite old and falling out of support.
    C++ 5.9 does have the fix.

  • I need sorting VI that returns the sorted array element's positions in the unsorted array

    I need a VI that will very quickly (n log n) sort a 1D array of doubles.  This VI should not only output the sorted array, but for each of the sorted array elements, it should also output that sorted array element's position in the unsorted array.  So for example if the input array were:
    unsorted_array
    4.1
    2.3
    7.0
    5.6
    10.2
    Then the output arrays would be:
    sorted_array
    2.3
    4.1
    5.6
    7.0
    10.2
    indices_array
    1 (the value 2.3 is located at position 1 in the unsorted_array)
    0 (the value 4.1 is located at position 0 in the unsorted_array)
    3 (the value 5.6 is located at position 3 in the unsorted_array)
    2 (the value 7.0 is located at position 2 in the unsorted_array)
    4 (the value 10.2 is located at position 4 in the unsorted_array)
    This way I could generate the sorted_array using just the indices_array and the unsorted_array.  Has anyone written a nifty piece of code to do this?  I did some research on (n log n) sorting algorithms but most of them use recursion which I don't know how to do in LabVIEW.  It would also be nice to have an algorithm like this that could sort an array of strings.
    cheers,
    Richard

    Try something like the attached example (LabVIEW 7.0). I think it does what you need.
    (To understand the code, it is important to know that arrays of clusters are sorted by the first element in the cluster).
    Sort array itself sorts also array of strings, so you could just substitute, keeping the rest of the code the same.
    Sort array uses a variation of quicksort, so it should have about NlogN complexity.
    (If you think you only need the array of indices to later generate the sorted array, it does not make much sense to even do it. To get the indices, you need to sort the array anyway. You should just sort the plain array directly.
    Message Edited by altenbach on 07-13-2005 03:47 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    SortAndIndex.vi ‏26 KB

  • Sorting array

    I know how to sort the array of strings alpha numerically but my problem is that the numerical values will not follow there corresponding strings
    here is the code i am using:
    Harold Timmis
    [email protected]
    Orlando,Fl
    *Kudos always welcome
    Solved!
    Go to Solution.
    Attachments:
    testingfileIO.vi ‏35 KB

    Hi.
    It's nice to have "sorting array" icon.
    But I have problem with programming similar option. I want to sort my sine samples taken in more periods to just one period.
    Beside defining that mycurrent sample should be less than next one, and greater from previous one in one case, I should take in consider that derivation could be positive or negative. While negative derivation, next sample should be less then current and so one.
    Can someone help me with programming?
    I've extracted the first period, and then I have to go trough each sample and compare him. When logic is well,I get the index and putinsert elemt in array.
    I think that I'm having problem with while loops. I don't know where to put them.
    I'm attaching VI. beacuse I'm working on Labview 9.0, I'm attaching also a pic.
    Thanks in advance.
    Attachments:
    resampling.vi ‏89 KB
    Untitled.jpg ‏135 KB

  • Sorting array list

    can u please give me the source code for:
    sorting array list for ascending and descending order

    You already have the source code.

  • Sorting array vs sorting paginated array

    ...pulling in a long list of photos in a gallery, and I have
    a sort function working within the pages of data fine. I need to
    bring it back out of the paging stuff so that it sorts the data,
    THEN passes to the paging function.
    My data comes from mysql > php > xml > Flex into an
    ArrayCollection, referred to in MXML as {acSlides}.
    Pass the array collection to the paging system like:
    <utils:FXCPager id="fxcPager" pageIndex="{currentPage}"
    pageSize="{pageSize}" dataProvider="{acSlides}" />
    then assign in repeater:
    <mx:Repeater id="slideRepeater"
    dataProvider="{fxcPager.pageData}">...</>
    There is a button for sorting that calls a function a la:
    private function sortByRating():void {
    var sort:Sort = new Sort();
    sort.fields = [new SortField("rating",false,true)];
    fxcPager.pageData.sort = sort;
    toggleRating = false;
    fxcPager.pageData.refresh();
    What do I need to do to get this sort function to work on the
    {acSlides} array collection instead of {fxcPager.pageData} ???
    Thanks!

    OK I have resolved the error by importing the class adGroup. However now I am back to the original problem which is how to sort the DisplayName alphabetically and keep the associated link in tact. I created the class to avoid sorting parellel arrays but I am afraid it solved nothing as I have no idea how to sort the class. I tried just sorting it like an array:
      Arrays.sort(bAD.getGroups()but receive the following error:
    java.lang.ClassCastException: com.mylbean.adGroup incompatible with java.lang.Comparable
    at java.util.Arrays.mergeSort(Arrays.java:1160)
    at java.util.Arrays.sort(Arrays.java:1131)
    >
    I would assume this message is caused because I cannot sort a class array using this method. But being newer to Java I really have no idea how to get the class to sort by DisplayName before displaying to the user.
    So at this point the code works fine but how do I display alphabetically? All suggestions are appreciated.

  • Data from array to JTable

    hi...
    i have array, containing data values & i want that values in JTable....
    plz help...
    i want simple help which i will use in my program
    Given code are one of the class of my file,i have to work on these codes(but don't consider it much its for just idea):
    class v_swing1 extends Panel
              PChart pc6;
              static double[][][] arrD;      // assume these arrays contain data
              double[][][] resultarrD; //
              JButton b1;
              TextField ta;
              JTable table ; // finally i want some selected data of above
    // arrays in JTable
              Label l1;
              TableModel myData ;
              private LocalTableModel theTableModel;                         //t1.setEditable(false);
              private List<Row> theRows;
              String selectedData=null;
         public Dimension getPreferredSize() // Dimension
         {return new Dimension(800,600);}
         public Dimension preferredSize()
              {return getPreferredSize();}
         private class Row {
              private String d1,d2,d3,d4,d5,d6,d7,d8;
              public Row(String s1, String s2,String s3,String s4,String s5,String s6,String s7,String s8) {
                   d1=s1;
                   d2=s2;d3=s3;d4=s4;d5=s5;d6=s6;d7=s7;d8=s8;
              public String get1()      {
                   return d1; }
              public void set1(String d1 ) {
                   this.d1= d1; }
              public String get2() {
                   return d2; }
              public void set2(String d2) {
                   this. d2 = d2; }
              public String get3() {
                   return d3; }
              public void set3(String d3) {
                   this. d3 = d3; }
              public String get4() {
                   return d4; }
              public void set4(String d4) {
                   this.d4 = d4; }
              public String get5()      {
                   return d5; }
              public void set1(String d5 ) {
                   this.d5= d5; }
              public String get6()      {
                   return d6; }
              public void set1(String d6 ) {
                   this.d6= d6; }
              public String get7()      {
                   return d7; }
              public void set1(String d7 ) {
                   this.d7= d7; }
              public String get8()      {
                   return d8; }
              public void set1(String d8 ) {
                   this.d8= d8; }
         private class LocalTableModel extends AbstractTableModel {
              public String getColumnName(int column) {
                   if (column == 0) {return "Speed S";}
              else if(column == 1) {return "Discharge Q";}
              else if(column == 2) {  return "Head H";}
              else if(column == 3){  return "Torque T";}
              else if(column == 4) {  return "Unit Spd n1";}
              else if(column == 5) {  return "Unit Disch. Q1 ";}
              else if(column == 6) {  return "Power I/N";}
              else if(column ==7) {  return "Power O/P";}
                   else {  return "Efficiency";}
              public Class getColumnClass(int columnIndex) {
              if (columnIndex == 0) { return String.class;}
         else     if (columnIndex == 1) { return String.class;}
         else     if (columnIndex == 2) { return String.class;}
         else     if (columnIndex == 3) { return String.class;}
         else     if (columnIndex == 4) { return String.class;}
         else     if (columnIndex == 5) { return String.class;}
         else     if (columnIndex == 6) { return String.class;}
         else     if (columnIndex == 7) { return String.class;}
              else      { return String.class; }
              public boolean isCellEditable(int rowIndex, int columnIndex) {
                   return true;     }
              public int getRowCount() {
                   return theRows.size();
              public int getColumnCount() {
                   return 8;
              public Object getValueAt(int rowIndex, int columnIndex) {
                   Row row = theRows.get(rowIndex);
                   if (columnIndex == 0) { return row.get1();}
                   if (columnIndex == 1) { return row.get2(); }
                   if (columnIndex == 2) { return row.get3(); }
                   if (columnIndex == 3) { return row.get4(); }
                   if (columnIndex == 4) { return row.get5(); }
                   if (columnIndex == 5) { return row.get6(); }
                   if (columnIndex == 6) { return row.get7(); }
                   else { return row.get8();}
              public void setValueAt(Object value, int rowIndex, int columnIndex){
                        Row row = theRows.get(rowIndex);
                   if (columnIndex == 0) { row.set1((String)value);}
                   if (columnIndex == 1) { row.set2((String)value);}
                   if (columnIndex == 2) { row.set3((String)value);}
                   if (columnIndex == 3) { row.set4((String)value);}
                   if (columnIndex == 4) { row.set5((String)value);}
                   if (columnIndex == 5) { row.set6((String)value);}
                   if (columnIndex == 6) { row.set7((String)value);}
                   if (columnIndex == 7) { row.set8((String)value);}
                   fireTableCellUpdated(rowIndex,columnIndex);
         public v_swing1(PChart pc6) // L.52
              {this.pc6=pc6;
              b1=new JButton("get Table");
              l1=new Label("No. of Data",Label.RIGHT);
              t1=new TextField(4);
              arrD=new double[15][15][15];
              resultarrD=new double[15][15][15];
              ta=new TextField(1);
              GridBagLayout glayout=new GridBagLayout();
              setLayout(glayout);
              GridBagConstraints gc=new GridBagConstraints();
              gc.fill= GridBagConstraints.HORIZONTAL;
              theRows = new LinkedList<Row>();
              theTableModel = new LocalTableModel();           
              table = new JTable(theTableModel);
              add(table);
              setLayout(new FlowLayout());
              b1.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                        load();
                        reload();                         
              add(new JScrollPane(table));
              load();               
         private void reload(){
              int l =Integer.parseInt(t1.getText());
              for(int size = theRows.size();size<=(l-1);size++)
              theRows.add(new Row(" "+(size)," "," "," "));
              theTableModel.fireTableRowsInserted(size, size);
         private void load(){
              int size = theRows.size();
              if (size > 0) {
                   theRows.clear();
                   theTableModel.fireTableRowsDeleted(0, size - 1);
              theRows.add(new Row(" "+0, " "," "," "));     
              theTableModel.fireTableRowsInserted(0, 1);
              static double[][][] returnarrD()
              { return arrD;
              }

    hi...
    i have array, containing data values & i want that values in JTable....
    plz help...
    i want simple help which i will use in my program
    Given code are one of the class of my file,i have to work on these codes(but don't consider it much its for just idea):
    class v_swing1 extends Panel
              PChart pc6;
              static double[][][] arrD;      // assume these arrays contain data
              double[][][] resultarrD; //
              JButton b1;
              TextField ta;
              JTable table ; // finally i want some selected data of above
    // arrays in JTable
              Label l1;
              TableModel myData ;
              private LocalTableModel theTableModel;                         //t1.setEditable(false);
              private List<Row> theRows;
              String selectedData=null;
         public Dimension getPreferredSize() // Dimension
         {return new Dimension(800,600);}
         public Dimension preferredSize()
              {return getPreferredSize();}
         private class Row {
              private String d1,d2,d3,d4,d5,d6,d7,d8;
              public Row(String s1, String s2,String s3,String s4,String s5,String s6,String s7,String s8) {
                   d1=s1;
                   d2=s2;d3=s3;d4=s4;d5=s5;d6=s6;d7=s7;d8=s8;
              public String get1()      {
                   return d1; }
              public void set1(String d1 ) {
                   this.d1= d1; }
              public String get2() {
                   return d2; }
              public void set2(String d2) {
                   this. d2 = d2; }
              public String get3() {
                   return d3; }
              public void set3(String d3) {
                   this. d3 = d3; }
              public String get4() {
                   return d4; }
              public void set4(String d4) {
                   this.d4 = d4; }
              public String get5()      {
                   return d5; }
              public void set1(String d5 ) {
                   this.d5= d5; }
              public String get6()      {
                   return d6; }
              public void set1(String d6 ) {
                   this.d6= d6; }
              public String get7()      {
                   return d7; }
              public void set1(String d7 ) {
                   this.d7= d7; }
              public String get8()      {
                   return d8; }
              public void set1(String d8 ) {
                   this.d8= d8; }
         private class LocalTableModel extends AbstractTableModel {
              public String getColumnName(int column) {
                   if (column == 0) {return "Speed S";}
              else if(column == 1) {return "Discharge Q";}
              else if(column == 2) {  return "Head H";}
              else if(column == 3){  return "Torque T";}
              else if(column == 4) {  return "Unit Spd n1";}
              else if(column == 5) {  return "Unit Disch. Q1 ";}
              else if(column == 6) {  return "Power I/N";}
              else if(column ==7) {  return "Power O/P";}
                   else {  return "Efficiency";}
              public Class getColumnClass(int columnIndex) {
              if (columnIndex == 0) { return String.class;}
         else     if (columnIndex == 1) { return String.class;}
         else     if (columnIndex == 2) { return String.class;}
         else     if (columnIndex == 3) { return String.class;}
         else     if (columnIndex == 4) { return String.class;}
         else     if (columnIndex == 5) { return String.class;}
         else     if (columnIndex == 6) { return String.class;}
         else     if (columnIndex == 7) { return String.class;}
              else      { return String.class; }
              public boolean isCellEditable(int rowIndex, int columnIndex) {
                   return true;     }
              public int getRowCount() {
                   return theRows.size();
              public int getColumnCount() {
                   return 8;
              public Object getValueAt(int rowIndex, int columnIndex) {
                   Row row = theRows.get(rowIndex);
                   if (columnIndex == 0) { return row.get1();}
                   if (columnIndex == 1) { return row.get2(); }
                   if (columnIndex == 2) { return row.get3(); }
                   if (columnIndex == 3) { return row.get4(); }
                   if (columnIndex == 4) { return row.get5(); }
                   if (columnIndex == 5) { return row.get6(); }
                   if (columnIndex == 6) { return row.get7(); }
                   else { return row.get8();}
              public void setValueAt(Object value, int rowIndex, int columnIndex){
                        Row row = theRows.get(rowIndex);
                   if (columnIndex == 0) { row.set1((String)value);}
                   if (columnIndex == 1) { row.set2((String)value);}
                   if (columnIndex == 2) { row.set3((String)value);}
                   if (columnIndex == 3) { row.set4((String)value);}
                   if (columnIndex == 4) { row.set5((String)value);}
                   if (columnIndex == 5) { row.set6((String)value);}
                   if (columnIndex == 6) { row.set7((String)value);}
                   if (columnIndex == 7) { row.set8((String)value);}
                   fireTableCellUpdated(rowIndex,columnIndex);
         public v_swing1(PChart pc6) // L.52
              {this.pc6=pc6;
              b1=new JButton("get Table");
              l1=new Label("No. of Data",Label.RIGHT);
              t1=new TextField(4);
              arrD=new double[15][15][15];
              resultarrD=new double[15][15][15];
              ta=new TextField(1);
              GridBagLayout glayout=new GridBagLayout();
              setLayout(glayout);
              GridBagConstraints gc=new GridBagConstraints();
              gc.fill= GridBagConstraints.HORIZONTAL;
              theRows = new LinkedList<Row>();
              theTableModel = new LocalTableModel();           
              table = new JTable(theTableModel);
              add(table);
              setLayout(new FlowLayout());
              b1.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                        load();
                        reload();                         
              add(new JScrollPane(table));
              load();               
         private void reload(){
              int l =Integer.parseInt(t1.getText());
              for(int size = theRows.size();size<=(l-1);size++)
              theRows.add(new Row(" "+(size)," "," "," "));
              theTableModel.fireTableRowsInserted(size, size);
         private void load(){
              int size = theRows.size();
              if (size > 0) {
                   theRows.clear();
                   theTableModel.fireTableRowsDeleted(0, size - 1);
              theRows.add(new Row(" "+0, " "," "," "));     
              theTableModel.fireTableRowsInserted(0, 1);
              static double[][][] returnarrD()
              { return arrD;
              }

  • CompareTo and Sorting Arrays

    I am trying to sort an array of telephone directory information by last, first names, but am having trouble understanding how to use the compareTo method to do so. I thought I could use Arrays.sort, but this doesn't seem to work....any help would be appreciated.
    public static Person [] sort(Person [] people) {
    Person [] sorted = Arrays.sort(people);
    return (Person[])sorted.toArray(new Person[sorted.size()]);
    }/* end sort method */
    //From Person class
    public int compareTo(Object o) {
    Person p = (Person)o;
    int temp;
    if((temp=getLastName().compareTo(p.getLastName())) == 0)
    if((temp=getFirstName().compareTo(p.getFirstName())) == 0)
    //not sure what to do here
    return temp;
    }

    You want to return a negative or positive or zero integer if this item is less than, greater than, or the same as, the object passed as an argument, right? (Read the API.)
    You're trying to sort by last name first, and if they're the same, then the first name, right?
    So you're not really checking for zero, you're checking for non-zero.
    I'm assuming that first name and last name are given by strings, which implements Comparable. So you can do:
    public int compareTo(Object o) {
      Person p = (Person)o;
      int temp = getLastName().compareTo(p.getLastName());
      if (temp != 0)
        return temp;
      // implicit else here
      // MORE CODE GOES HERE
    }I'll let you do the "MORE CODE GOES HERE" as an exercise.
    BTW if you're implementing Comparable, you should probably be overriding equals() as well.

  • Index of sorted array

    I have a unsorted array as follows:
    array_index = {0 1 2 3 4 5 6 7 8}
    array = {1 2 4 3 4 3 4 2 1}
    I would like to get the previous index of sorted array as follows:
    old_index = {2 4 6 3 5 1 7 0 8}
    arrray.sort doesn't return the index the previous index, please help me.

    You can do that using by creating an array containing the numbers 0 to n in increasing order and sorting that using a comparator that actually compares the content of the original array at the indices provided to it instead of the values themselves.
    But why would you need that? Could you describe your requirement in a broader scope?

  • Sort Array of References by Label-Name

    I am developing on an DSC application with shared variables and alarming and events with a hugh amount of variables. Each variable is displayed on the front panel within tab controls. To reduce time and effort when expanding the variables I would like to arrange my references from the front panel by the label name. I've attached a part of the intialization of the front panel. There I have to manually expand each variable in the shared variable library, where the name of the variable has to be in the same order as the definition in the cluster.
    My approach was to build an array of references of digital type and sort the references by the label name. Because the shared variables have the same name as the controls they would be in the same order and I also could dislaim the cluster of references.
    maybe somebody can give my an hint.
    Kind Regards,
    Joachim
    Solved!
    Go to Solution.
    Attachments:
    fp.jpg ‏982 KB

    Hi Joachim,
    use the standard "sort an array of cluster" approach!
    Put the control label and the reference (in this order!) in a cluster, make an array of such clusters and sort the array. It will sort by the label and you can unbundle the reference in sorted order...
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • Sorting array not working when run as a job

    I have a Powershell script that reads data from a log file, extracts a username and adds it to an array. I then sort the array to remove duplicates and export the data to a CSV file. The script works fine when run locally on the server but I need to call
    the script remotely for a number of servers and when I do this it does not output anything to the CSV. The script is run as a job.
    $ArrList = [System.Collections.ArrayList]@()
    # open file and read data
    $arrlist.Add("$final")
    $arrlist | sort -unique | out-file c:\scripts\iis_users.csv
    If I change the script to use add-content it can add entries to the same CSV file. But ideally I need to sort the output as there are many duplicates.

    Not sure why but that would only export the last user. I didn't have time to investigate so instead I decided to only add users to the array after first checking they didn't exist already:
    if ($arrlist -notcontains $final)
    $arrlist.Add("$final")
    add-content c:\scripts\iis_users.csv "$final"
    I thought this would create a performance hit but it seems almost as fast.

  • How to Sort Array of Object

    Hallo. I have an array of objects. Each object has id and name. How can i sort by name or by id??
    Thx for all
    Max

    array.sortOn("name");

Maybe you are looking for

  • Print error on HP LaserJet 4 Plus

         I have a Macbook Air, Mac OS X 10.6.8 and have installed my old trusty Laserjet 4 Plus printer. It printed once, then gave me an error, "W2 INVALID PERS" and won't print again. I am told I do not need to download another driver, the Air has what

  • GPS very slow and GPS question

    Hello, Is this happen only with my X6? Its take the GPS more then two minuts to get the location even when I see that it found 5 satellites with good sign. Also, I wanted to know - I have a ploar sport tracker program that use the GPS. If I start the

  • Header text changing automatically in the BADI

    Hi, I am using a class in the BADI IF lt_item[] IS NOT INITIAL AND lt_partner[] IS NOT INITIAL AND it_account[] IS NOT INITIAL.    zcl_sc=>set_location_detail(      CHANGING        sc_items    = lt_item        sc_partners = lt_partner        sc_accou

  • Acrobat form

    hi, I have a form in document has 2 columns: "yes" and "no". The user of the pdf form clicks yes or no and at the bottom, the form must give the numerical total of yes and no answers. can u pls help me? thanks Ali

  • Internal error, while printing from a IE (IE 10, 11)

    Helo, issue: while printing from an webbrowser IE 11, than I get an error: - There was an internal error, and Internet Explorer is unable to print this document- Please, who knows an solution so that I can resolve the problem. P. Verboom