Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:43:03+00:00 2026-06-18T02:43:03+00:00

I have an html list <select> <option value=w count=1>w (1)</option> <option value=a count=1>a (1)</option>

  • 0

I have an html list

<select>
  <option value="w" count="1">w (1)</option>
  <option value="a" count="1">a (1)</option>
  <option value="d" count="5">d (5)</option>
  <option value="r" count="0">r (0)</option>
  <option value="q" count="0">q (0)</option>
  <option value="s" count="0">s (0)</option>
  <option value="b" count="5">b (5)</option>
</select>

I want the list to be order by count first, then by value.

Output:

<select>  
  <option value="b" count="5">b (5)</option>
  <option value="d" count="5">d (5)</option>
  <option value="a" count="1">a (1)</option>
  <option value="w" count="1">w (1)</option>
  <option value="q" count="0">q (0)</option>
  <option value="r" count="0">r (0)</option>
  <option value="s" count="0">s (0)</option>
</select>

I know how to sort by count OR by value but I don’t find an easy way to combine them.

Sort by count:

select.sort(function (a, b) {
    var c1 = parseInt($(a).attr('count'), 10);
    var c2 = parseInt($(b).attr('count'), 10);
    return c1 < c2 ? 1 : c1 > c2 ? -1 : 0;
});

and by value

select.sort(function (a, b) {
    return $(a).text().toUpperCase().localeCompare($(b).text());
});

I am thinking about building an object:

var map = {
    5: ['d', 'b'],
    1: ['a', 'w'],
    0: ['r', 'q', 's']
};

Then sort each keys:

function keys(map) {
    var keys = [];
    for (var key in map) {
        if (map.hasOwnProperty(key)) map[key].sort();
    }
    return keys;
}

keys(map);
map;

And finally reconstruct the select list.

Is there an easier way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:43:05+00:00Added an answer on June 18, 2026 at 2:43 am

    Try this:

    select.sort(function (a, b) {
        var c1 = parseInt($(a).attr('count'), 10);
        var c2 = parseInt($(b).attr('count'), 10);
    
        if(c1 === c2){ // If the counts are equal, return the comparison for value.
            return $(a).text().toUpperCase().localeCompare($(b).text());
        }
        // Otherwise, compare the counts.
        // (With numbers, you can just substract `b` from `a` in sort functions.)
        return c1 - c2;
    });
    

    With the comments removed, that’s just 6 lines of code.

    There’s no need to wrap the second return in a else, because the sort function won’t ever evaluate an else, if c1 === c2, since the if contains a return statement.

    Now, the sort function expects X<0, X=0 or X>0 to be returned (Where X is the output), depending on if a is less than, equal to, or greater than b. If you subtract b from a, you get the values you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this HTML: <select id='list'> <option value='1'>First</option> <option value='2'>Second</option> <option value='3'>Third
In an HTML page i have severals list. <select name=salut-1358937506000-OK> <option selected= value=OK>OK</option> <option
I have a html select list menu,when user select an option he/she is redirected
I have a HTML dropdown list with single selection. I want to select a
I want to specify a value for each option in the my select list
Hi I have a Select list liek so: <select id=cardtype name=cardtype> <option selected='y' value=debit-0.00-50.00>Debit1</option>
I have this html on a web page: <select name=chapter_select class=chapter_select> <option value=http://www.example.com/chapter/1>Chapter 1</option>
I have a html form which have a select list box from which you
I have multiple dropdown list: @Html.DropDownListFor(x => x.HaveColoSpace.SelectedOptions, new SelectList(Model.HaveColoSpace.Options, Value, Text), new {
So if you have a html List Box, also called a multiple select, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.