Results 1 to 7 of 7
  1. #1
    santanu is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default Sorting in descending order

    I am basically generating the RSSI values(e.g., 24, 16, 32, 38.....) in the RSSI column in a table using return history.getRssi(); in case 1 of the code. Now the data is displaying in random order. I want to display them in Descending order. Can anybody help me how to do this as I am new in Java development. I am setting thr value for RSSI column using history.setRssi( tagHistory.getRssi() ); in the setData() method. For your convenience I am putting the setData() method and the code where I am displaying the values of RSSI column.

    ************************************************** ********
    public void setData( Tag _tag, WardenEvent[] _events, Device[] devices )
    {
    histories = null;
    if( _tag != null )
    {
    History[] tagHistories = _tag.getHistories();
    histories = new History[tagHistories.length];
    int k = 0;
    for( int j = 0; j < tagHistories.length; j++ )
    {
    History tagHistory = tagHistories[j];
    History history = new History();
    history.setCheckinTS( tagHistory.getCheckinTS() );
    history.setRssi( tagHistory.getRssi() );
    history.setDeviceId("");
    boolean add = false;
    if( devices != null )
    {
    for( int i=0; i<devices.length; i++ )
    {
    Device device = devices[i];
    if( device.getGUID().equals( tagHistory.getDeviceId() ) && device.getType() != Device.EAC )
    {
    history.setDeviceId( device.getName() );
    add = true;
    break;
    }
    }
    }
    if( add )
    {
    histories[k] = history;
    k++;
    }
    }
    for( ; k<tagHistories.length; k++ )
    {
    History EmptyHistory = new History();
    EmptyHistory.setCheckinTS( "" );
    EmptyHistory.setRssi( "" );
    EmptyHistory.setDeviceId( "" );
    histories[k] = EmptyHistory;
    }
    }
    tag = _tag;
    events = _events;
    }

    ************************************************** ****

    The code where I am displaying the RSSI values:

    public Object getValueAt( int row, int col )
    {
    if( null != histories && 0 <= row && row < getRowCount() )
    {
    try
    {
    History history = histories[row];
    switch( col )
    {
    case 0:
    return ( history.getCheckinTS().length() > 5 ) ? history.getCheckinTS().substring(5) : history.getCheckinTS();
    case 1:
    return history.getRssi();
    case 2:
    return history.getDeviceId();
    } // switch
    }
    catch( Exception e )
    {
    System.out.println( "exception:" + e.toString() );
    System.out.println( e.getStackTrace().toString() );
    }
    } // if ( null != tags && 0 <= row && row < tags.length )
    return "";
    } // public Object getValueAt( int row, int col )

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    The simplest and usual way to sort data in Java is to use one of the methods Collections.sort() or Arrays.sort(), depending on whether you're sorting an array or a collection such as a List.

    If you can, the simplest approach here would be to make the History class implement Comparable (see javadoc). Make the compareTo() method compare the RSSI value and return an appropriate value. Then pass in your array of History objects to Arrays.sort().

    A more flexible approach is to make the sorting happen inside your table model, e.g. by making getValueAt() have an index of 'which row in sorted order corresponds to the row being requested'. But maybe if you're new to Java this isn't the easiest approach.

    If you're using Java 6, you may also want to look at the TableRowSorter class.

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    P.S. To sort in REVERSE order, either:
    (a) make your compareTo() method just return the right values to make this happen;

    (b) instead of making History implement Comparable, create a separate Comparator object:
    Comparator<History> comp = new Comparator<History> {
    public int compare(History h1, History h2) {
    return (h1.rssi < h2.rssi) ? - 1 : ((h1.rssi > h2.rssi) ? 1 : 0);
    }
    }
    Then, whereas normally you'd pass in the comparator like this for ascending order:

    Arrays.sort(histories, comp);

    Instead, call it like this for reverse (descending) order:

    Arrays.sort(histories, Collections.reverseOrder(comp));

    Hope that helps
    Neil

  4. #4
    santanu is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default sorting in descending order

    I tried to implement the following way to sort the RSSI values in descending order. Basically I wrote the compare method and call Arrays.sort() method from setData() method. But still the data are in random order, not in Descending order. Please suggest me how I can sort the RSSI values in Descending order. For your convenience I put my new implementation below:

    public void setData( Tag _tag, WardenEvent[] _events, Device[] devices )
    {
    histories = null;
    if( _tag != null )
    {
    History[] tagHistories = _tag.getHistories();
    histories = new History[tagHistories.length];
    int k = 0;
    for( int j = 0; j < tagHistories.length; j++ )
    {
    History tagHistory = tagHistories[j];
    History history = new History();
    history.setCheckinTS( tagHistory.getCheckinTS() );
    history.setRssi( tagHistory.getRssi() );
    history.setDeviceId("");
    boolean add = false;
    if( devices != null )
    {
    for( int i=0; i<devices.length; i++ )
    {
    Device device = devices[i];
    if( device.getGUID().equals( tagHistory.getDeviceId() ) && device.getType() != Device.EAC )
    {
    history.setDeviceId( device.getName() );
    add = true;
    break;
    }
    }
    }
    if( add )
    {
    histories[k] = history;
    k++;
    }
    }
    for( ; k<tagHistories.length; k++ )
    {
    History EmptyHistory = new History();
    EmptyHistory.setCheckinTS( "" );
    EmptyHistory.setRssi( "" );
    EmptyHistory.setDeviceId( "" );
    histories[k] = EmptyHistory;
    }
    }
    tag = _tag;
    events = _events;

    Arrays.sort(histories, new RssiComparator());
    }

    *************************************************

    class RssiComparator implements Comparator
    {
    public final int compare( Object a, Object b )
    {
    History tagA = (History)a;
    History tagB = (History)b;
    return tagA.getRssi().compareTo( tagB.getRssi() );
    }
    }

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    At a glance the code looks OK -- I assume the RSSIs are Strings, then? What do they actually look like -- is the ordering definitely "random", as opposed to "how you'd expect strings to be ordered" (bearing in mind the ordering is essentially by length then 'raw' character value)?
    Neil

  6. #6
    santanu is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default sorting in descending order

    Yes, you are right. The RSSIs are Strings. Now they are generating randomly like 24, 16, 32, 38..... I want to sort them in descending order. Please suggest me...

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Sorting and searching are at the core of computer science. Often for such a problem it is better to be stuck awhile ..... maybe you are not getting any progress that way.

    Consider: if a value, any of the values, is loaded into a variable. Then we go to the next one. If next < last == actually the current then we do last = next ~ you will have to change the variable names.

    But if we pick one, any one, and go through the array replacing the one we pick with any that are smaller, we end with the smallest value.

    No?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Descending order
    By santanu in forum New To Java
    Replies: 1
    Last Post: 11-04-2008, 05:33 PM
  2. Display the data in Descending order
    By santanu in forum New To Java
    Replies: 2
    Last Post: 10-31-2008, 01:06 PM
  3. Tab order on swing components
    By ashvin@projectdemo.biz in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2008, 11:06 AM
  4. How to place the GUI components in correct order
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-04-2008, 07:41 AM
  5. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •