Sorting Hashtable by value

All,
I have a Hashtable with key (String) and value(float) now I want to sort it by value without disturbing the key value match
I know, it can be done by sorting the keys... and then calling get() to obtain the value for the key but I want exactly opposite..
Thanks
Vishal

Set set = map.entrySet();
Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
Arrays.sort(entries, new Comparator() {
          public int compare(Object o1, Object o2) {
     Object v1 = ((Map.Entry) o1).getValue();
     Object v2 = ((Map.Entry) o2).getValue();
     return ((Comparable) v1).compareTo(v2);
});And how would one genericise(?) the above code snippet?
I have the following attempt but I continue to receive one unchecked warning.
Set<Map.Entry<String, String>> set = map.entrySet();
Map.Entry[] entries = set.toArray(new Map.Entry[set.size()]);
Arrays.sort(entries, new Comparatorlt;Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> e1, Map.Entry<String, String> e2) {
            String v1 = e1.getValue();
            String v2 = e2.getValue();
            return v1.compareTo(v2);
        });

Similar Messages

  • Need help Sorting a Hashtable using values??

    I have a hastable whose keys are unique but values are not. The problem is that i have to sort the hashtable by values.

    You can't really sort a Hashtable by values, at least not directly. What you probably want is a way to be able to iterate through all the values contained in the Hashtable in a way that is consistent with some order defined on those values. An easy way to do this goes as follows:
    Map  m;   // let that be your Hashtable
    List  vals = new ArrayList(m.values());
    Collections.sort(vals);  // uses the values natural order OR
    Collections.sort(vals, my_comparator);  // uses the order defined by my_comparator
    // this will iterate in the order defined on the values
    Iterator  it = vals.iterator();
    while(it.hasNext()) {
      Object val = it.next();
    }

  • How to sort  the arry value in ascending order

    I have a string array where i need to sort the below values in ascending order in the same format...can anyone give me clue on this?
    9.3
    3.1
    9.1
    19.1
    19
    9.4
    9.1.1
    the sorted order should be
    3.1
    9.1
    9.1.1
    9.3
    9.4
    19
    19.1

    You may have easier luck writing your own comparator for this. These are headings in a table of contents, right?
    Write a comparator that tokenizes a string on the '.' character, or use a StringBuffer to remove them, and then order the elements according to the combined numbers. Since alphanumeric would order this list as you want it anyway you could just order that way once the '.' are removed.
    In other words your comparator would do this in the "compare" method:
    public class MyComparator implements Comparator, java.io.Serializable {
    private static Comparator stringComparator = java.text.Collator.getInstance();
    ...constructor(s), your own private "instance" variable of type MyComparator, a getInstance() method of your own, yadda yadda...
    public int compare(Object item1, Object item2) {
    try {
    value1 = removePeriods((String)item1);
    value2 = removePeriods((String)item2);
    if (value1 == null) {
    return (value2 == null) ? 0 : (-1);
    return compare(value1, value2);
    } catch (ClassCastException cce) {
    System.err.println("Wrong Object Type, JackAss!");
    protected int compare(String str1, String str2) {
    return MyComparator.stringComparator.compare(str1, str2);
    private String removePeriods(String value) {
    StringBuffer sb = new StringBuffer(value);
    int decimalIndex = value.indexOf('.');
    while (decimalIndex != -1) {
    sb.delete(decimalIndex, (decimalIndex + 1));
    }

  • Query needed for sorting by time value

    Hi Folks
    We have table it consists two columns like below
    Job_name varchar2(50)
    Scheduled_time date
    The filed scheduled time keep date and time value like below
    25-Jul-2009 4:00:10 AM
    26-Jul-2009 4:00:01 PM
    27-Jul-2009 4:00:00 PM
    28-Jul-2009 4:05:00 PM
    01-Jul-2009 4:06:00 PM
    02-Jun-2009 4:15:40 AM
    We need output as sorting by time value NOT date value. Expected output to be below
    25-Jul-2009 4:00:10 AM
    02-Jun-2009 4:15:40 AM
    27-Jul-2009 4:00:00 PM
    26-Jul-2009 4:00:01 PM
    28-Jul-2009 4:05:00 PM
    01-Jul-2009 4:06:00 PM
    I am using oracle 10G
    Thanks in Advance

    Here's how :
    SQL> create table job_table (job_name varchar2(50) not null, scheduled_time date);
    Table created.
    SQL> insert into job_table
      2  values ('Job_abc',to_date('25-JUL-2009 04:00:00','DD-MON-YYYY HH24:MI:SS'));
    1 row created.
    SQL> insert into job_table
      2  values ('Job_fdw',to_date('02-JUN-2009 04:15:40','DD-MON-YYYY HH24:MI:SS'));
    1 row created.
    SQL> insert into job_table
      2  values ('Job_fxj',to_date('27-JUL-2009 03:59:00','DD-MON-YYYY HH24:MI:SS'));
    1 row created.
    SQL> insert into job_table
      2  values ('Job_rjt',to_date('20-JUL-2009 14:59:00','DD-MON-YYYY HH24:MI:SS'));
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> alter session set nls_date_format='DD-MON-YYYY HH:MI:SS AM';
    Session altered.
    SQL> select job_name, scheduled_time from job_table
      2  order by to_char(scheduled_time,'HH24:MI:SS');
    JOB_NAME                                           SCHEDULED_TIME
    Job_fxj                                            27-JUL-2009 03:59:00 AM
    Job_abc                                            25-JUL-2009 04:00:00 AM
    Job_fdw                                            02-JUN-2009 04:15:40 AM
    Job_rjt                                            20-JUL-2009 02:59:00 PM
    SQL>

  • Can we apply sorting on Column values in Cross tab??

    Can we apply sorting on Column values in Cross tab??
    Following is the scenario with me.
    I have 2 fields and one formula.
    Fields are HostName and Username.
    Formula is status which categorizes the Authorized and Unauthorized events.
    And in the Summary Field, I am calculating the total number of events.
    In cross tab, Fields are marking the Rows of the cross tab and Formula is making the Columns(Authorized and Unauthorized ) of the cross tab.
    Since in cross tab data is grouped from left to right and sorted by default. I want to remove this default sorting and want to put sorting on the formula for Unauthorized events.
    Is there any way to fulfill this requirement?

    Hi,
    You can assign a number to each Unauthorised field e.g
    Rank   Unauthorised
    1         A
    2         BC
    3         DF
    5         TD
    6         GF
    Then add "Rank" to the crostab Columns before the Unauthorised  field. Then you can control the sorting order on your crosstab.
    Hope this helps
    Regards
    Dotun

  • How we can sort subtotal results value in abap alv report

    Hi, How we can sort subtotal results value in abap alv report

    Thanks a lot for your code
    but i am still getting double and weird results.
    Subtotal     IN     PARTY              KGS        TOTAL VALUE
         1     40008     3,141.20     192,799.00
         1     40008     16,681.06     1,908,659.00
    Subtotal     1          19,822.25     2,101,458.00
         10     40022     4,590.60     531,228.00
         10     40022     3,448.27     377,173.00
    Subtotal     10          8,038.87     908,401.00
         100     40010     270.172     19,852.00
    Subtotal     100          270.172     19,852.00
         101     40036     752.898     61,051.00
         101     40036     207.586     19,431.00
    Subtotal     101          960.484     80,482.00
         102     40048     325.936     32,154.00
         102     40048     264.32     19,364.00
    Subtotal     102          590.256     51,518.00
         103     40066     216.134     18,088.00
    Subtotal     103          216.134     18,088.00
         104     40001     231.96     16,986.00
    Subtotal     104          231.96     16,986.00
         105     40021     585.918     65,461.00
         105     40021     108.683     15,825.00
    Subtotal     105          694.601     81,286.00
         106     40046     209.777     15,341.00
    Subtotal     106          209.777     15,341.00
         107     40043     167.353     14,755.00
    Subtotal     107          167.353     14,755.00
         108     40046     153.023     14,343.00
         108     40046     342.348     32,223.00
    Subtotal     108          495.371     46,566.00
         109     40008     184.085     13,483.00
    Subtotal     109          184.085     13,483.00
         11     40011     5,275.63     524,232.69
         11     40011     6,723.28     643,911.82
    Subtotal     11          11,998.90     1,168,144.51
         110     40067     142.113     13,333.00
         110     40067     492.883     44,428.00
    Subtotal     110          634.996     57,761.00
         111     40040     118.961     13,190.00
         111     40040     492.433     60,029.00
    Subtotal     111          611.394     73,219.00
    Edited by: Timaji Sawant on Feb 16, 2012 12:16 PM
    Edited by: Timaji Sawant on Feb 17, 2012 9:27 AM

  • Sorting of Characteristics Values

    Hi Experts,
    I have one issue.When i am giving the values to the characteristic (Data format is CHAR)in the equipment (after assigned the class to the equipment)it is sorting the values example-1,10,3,5 even if I give 1,3,5,10.The requirement is it should be like 1,3,5,10 (as we input).Because of the CHAR format the system is sorting the values like this.If it is NUM format it is showing 1,3,5,10.
    Is there any way to stop the sorting of the values even if the data format is in CHAR?
    Regards
    Kalyan

    No.  For values there is no way to order them the way you want.  SAP sorts them.
    FF

  • Sorting Map on values rather than keys

    Is it possible that I can have a Map which is sorted based on values rather than keys ?
    If not is there an alternative.
    Basically I have keys as strings and values as integers.
    The values keep incrementing and at the end I want to get the top 50 keys based on the values.

    Sorting a Map by values is not a good idea, I did something similar before, it doesn't support the design, but it is better in performance, which is implementing a custom class (name it Record) that contains two members: key and value, create a record and insert it into a SortedSet e.g. TreeSet.
    make sure to do the following:
    1. This custom class must implement Comparable and compares two values
    2. Override euqlas method and accept strings (check using instanceof) then compare it to the key so you can retrieve the records using the key (to let it work as a map)
    Now you have a sorted set with key,value pairs, use the add method to insert a new pair, use the remove to retrieve the desired object and the set is already sorted :)
    Regards
    Mohammed

  • Sort Rowset field values without using rowset.sort or sortscroll

    Hi dudes,
    any one tell me the different ways to sort a rowset values ie, single field or more than one field without using Rowset.sort or Sortscroll() method. i tried some ways but not get the optimum solution.

    My first question to you would be why you want alternatives for Rowset.sort or Sortscroll.
    Your question doesn't describe your true issue or motive to search for alternatives for delivered PeopleTools Apis & Buildin Functions.

  • ForEach sort on Map values

    Hi,
    Is there any way to sort a map values using ForEach droplet.
    Thanks,

    Could you pls explain a little bit more.In order to sort Map elements by key, set the value as follows:
    {+|-}_key
    For example:
    <dsp:param name="sortProperties" value=" _key"/>See sortProperties section:
    http://docs.oracle.com/cd/E23095_01/Platform.93/PageDevGuide/html/s1316foreach01.html
    -RMishra

  • BO - Explorer  Sorting of 0calmonth values

    Is there any possibility to sort the 0calmonth values grouped by Year ?
    Data has been linked from a sap query based Universe. The Problem is that the Explorer system does not sort the values in the right direction. It should be like 11.2009, 12.2009, 01.2010 .....
    [http://www.abload.de/img/sortingzddw.jpg]

    Sorry... There is no custom sort in Explorer... What I've done once was using the Explore More... button to select the items I want, and the sequence in which items are selected matters, so this is a work around for custom sort.
    But in your case, you've got only 6 months, so there'll be no Explore More... option. I suggest change the format of the year month object. For example, put year in the front, so that Explorer will be able to properly sort it out.

  • How to sort via multi value parameter in ssrs

    I have a parameter with multi values. when the user selects ,he get of options like acount name, first name, last name.
    so if user selects firstname , the data has to sort by firstname.
    if he selects acount name , the data has to sort by account name.
    please help me how to sort the data using parameter with multivalue? in ssrs 2008.
    Thank you?

    Hi Venku,
    Based on your description, it seems that you create a parameter with available values and you want to sort the report data based on the parameter value. If the available values are the field names in the report, you can try to open the Tablix properties
    dialog box and specify the sort expression on "Sorting" tab as follows:
    =Fields(Parameter!parametername.value).value
    If the parameter allow multiple values and you want to sort the report data by more than one field, for example, sort the report data by "accountname" and then by "firstname", please add two sort expressions as follows:
    =Fields(Parameter!parametername.value(0)).value
    =Fields(Parameter!parametername.value(1)).value
    If you have any question, please let me know.
    Regards,
    Fanny Liu
    Fanny Liu
    TechNet Community Support

  • (Urgent) Sorting search help values in Web Report

    Hello Friends,
    We have a web report which uses a variable made on 0CALMONTH. The order of display of values for this variable search help is 02/2004, 03/2004, 01/2005, 07/2006, 08/2006, 05/2007. In this order we have the latest date (05/2007) displayed at the end of the list. What we want is that the values in the search help of 0CALMONTH should be displayed in the reverse order i.e. latest date first (05/2007, 08/2006, 07/2006... etc).
    Thanks in advance for your assistance.
    Regards,
    Prem.

    Hi Premanand,
    If u display the report in Internet Explorer there you can see differnet buttons like
    Save View, Book mark, Variable Screen etc...There click Save View give tech name and descryption. After Sorting use this in WAD instead selecting query , select this view and execute.
    Assign Points Pls...
    Best Regards,
    SG

  • Bug in sort of numeric values in ( title and album ) string.

    The Bug in the sort-function of the iPod results in mixed up track list.
    An album with more then 9 tracks will be sorted in the wrong way, when only title or filename or album field is just to give a track a number. The play order is 1, 10, 11, 12, 13, 2, 3 , 4 , 5, 6.... instead of the correct order 1,2,3,4,5,6...10,11, 12...
    For example: After playing the track named "The Beatles - Yellow Submarine - 1 - Yellow Submarine" the ipod will play "The Beatles - Yellow Submarine - 10 - Baby, You´re A Rich Man" instead of "The Beatles - Yellow Submarine - 2 - Hey Bulldog."
    The programmer have to search the string for numbers and join all numeric values to a single number.
    Example: Sample data :abc123. is "a" a number? no. is "b" a number no. is "c" a number no. is "1" a number? yes. store. is "2" a number? yes. join to "1". (now we have "12"). is "3" a number? yes. join to "12". (Now we have "123") -> EOL
    Is "123" largen than "99"? yes! is "123" larger than 124. no!
    Thats it. In perl you need 1 line of sourcecode, in c and c++ this can be done in 10 lines. In java (there a objects for this type of sort).
    And every user will be happy and nobody has to do the 01, 02, 03 workaround or reimport their audiobooks.
    Alternativly Apple should thing about storing the original filename in the ID3 tag or itunes database. This would make sorting the files without id3 tags (believe me or not, there are many people using mp3 for 10 years and have no id3 tags in there mp3-library) in itunes possible. (there is a god free tagger for mac os but no for windows.)
    A company like Apple should have the ability to correct the error / bug in the iPod-firmware (have the same issue in classic and iphone) or itunes-softwar.
    This is a wellknown and very common error done by newbie programmers or people who have not read the documentation of the software-libraries.

    I still have an H140 in the cupboard. As I recall that needs leading zeros on filenames to sort properly. Since both WMP & iTunes include leading zeros it was never really an issue. THE Rename should be able to extract the track numbers from part of the filename and is also able to generate counters, along with the usual functions of setting tag fields from part of the file and/or folder name. Unfortuanetly THE Rename is still on the complicated side - I suspect there would be no easy way to lump together the variety of options that it has while keeping it very simple.
    Once you've used an external program to adjust the tags you can make iTunes re-read them just by selecting the group of files in question, using CRTL-I to "Get Info." and then clicking OK +without making any changes+. iTunes will recheck it's database against any values in the tag and update as required. Given the lack of existing tags I would imagine that you don't let iTunes reorganise your music at the moment and would suggest you keep things that way at least until you are sure that all the tags have been correctly applied and you have a suitable backup. I stick to a Artist/Album/## Track structure which I don't let iTunes manage so that I can always recover details from the filesnames if I should ever accidentally edit more tracks than I intend.
    tt2

  • Can you sort a multi value field in descending order on a BC?

    Hi
    We have a date field which is a multivalue field on our Enrolment BC. The field comes from our Attendance BC. The Enrolment BC has a one to many relationship with an Attendance BC.
    We would like to sort this date in descending order for use on the Enrolment BC. We tried this on the link using the associated list sort spec. However this did not work.
    The only way we could get the multi value field to sort by descending order on the Enrolment BC was to add a sort spec on the Attendance BC. That is Date (DESC).
    Is there any other to do this using configuration? We don't want to add a sort spec on the Attendance BC to do this. At the moment it looks we will need to use script on the Enrolment BC.
    Thanks for you help,
    Tim

    That is a good suggestion. It looks like we already have a couple of cloned "Attendance" BC's used by workflow / interfaces so maybe I can use one of these. Thanks!

Maybe you are looking for

  • Document numbering series error

    Hi Experts, Our client has been using SBO 2005A for a long time but recently they are having document numbering series error regardless what users and even the manager account that has full authorization to all and a superuser cannot add document. No

  • Field value not being written to database

    Hello, I'm trying to auto-populate a form field from a value in one of my backing beans by changing the field value property from #{bindings.Modifiedby1.inputValue} to #{pageFlowScope.backing_Login.userName}. When I open the form, the correct value f

  • Flash10e.ocx version 10.0.45.2 or 10.0.42.34?

    First I had used the flash uninstal tool from adobe and successfully removed the older version of flash player I thought I downloaded flash player 10 version 10.0.45.2, but when I go into my browser IE7 and look at the manage add ons, I see Shockwave

  • Re: latest garbage from BT support - the "fault th...

    Openreach should fix it.. they can pay their staff your last 6 bill statements as payment! I Had the same problem with a business I knew. They had no BB  for 3 months as the line had been damaged by trees, Three months to organise tree cutters to tri

  • Should I use SharedObjects?

    Hi all, I'm making a desktop based program where I will need to save a lot of user information, and I am trying to figure out if SharedObjects is the answer. I do not plan on moving the application once it is installed on the user's computer either.