JavaScript switch case: anyway to force the order of the elements as they are written out?


I've got results being returned to a Google Mapping application in the div sidebar. The results are names of businesses that belong to categories that the client wants returned in a certain order. And unfortunately it's not a straight alpha sort. So one category begins with F and the second one with a C and the last three are A's, for example.

So I need my switch, which works, but naturally insists on dropping the values in alpha sorted (as they are returned from the DB that way) order as that's how it encounters them. What's the best way to set this up so I can grab my preferred categories with their associated company names in the arbitrary order the client has asked for?



Can you iterate over the categories in the order you want them in, and find the object to which it is associated?

E.g. (pseudocode)

var categories = [ 'F', 'C', 'A1', 'A2', 'A3' ].map(function (category) { return businesses.filter(function (business) { return business.category === category; }); });


So the missing step in the answer given here was HOW the map would be implemented and HOW the JS snippet could be implemented. Anyway, I ended up having to ask that as a separate question and finally got a nice working example for an answer.


Russ wrote:

The code given looks most likely to be using the jQuery JavaScript library that has some useful functions such as map() for manipulating arrays.

If we go back to the original problem, you need to order a list of categories based on the client's preference. Let's create a an object literal to map the ordering

</blockquote> var map = { F : 5, C : 3, A1 : 1, A2 : 4, A3 : 2 } <blockquote>

We can use this map to order the array using the sort method

</blockquote> var array = ['F', 'C', 'A1', 'A2', 'A3']; array.sort(function(a,b) { return map[a] - map[b]; }); This returns us ["A1", "A3", "C", "A2", "F"]

Anyway, I wanted to make sure this was included on this thread for anyone searching for this issue in the future or anyone following along right now. Thanks for everyone's input!


  • How to rename an IIS Express website in Visual Studio 2012
  • java.util.Scanner does not return to Prompt
  • How to prevent Excel from handling strings containing a colon as formulas
  • How to keep spaces within angle brackets of C++ casting operators in Visual C++ 2015?
  • Update statement containing aggregate not working in SQL server
  • jQuery data value not updating
  • Should ScheduledExecutorService.scheduleAt* methods re-schedule tasks if the task throws RuntimeExce
  • What is the difference between GetComponent ().enabled and .SetActive (false); in unity
  • Grails redirect with reverse proxy
  • Write “NOT IN” in Doctrine Query Language
  • Removing levels from data frame read from CSV file - R
  • Can someone please explain how this implementation of bucket sort works?
  • Select count of rows that have a certain number of rows in a related table
  • Youtube Video Player Fullscreen click event
  • Editable Combo Box Javascript and HTML
  • Python find continuous interesctions of intervals
  • Is it possible to send skype chat messages from a linux server without X?
  • Encrypting credit card details using AngularJS in Braintree
  • Autohotkey script running program with command line arguments
  • What version of Java should I use with Cassandra 2.0?
  • Create registry key in 32-bit hive on x64 PC using Installshield 2012 LE - Avoid redirection
  • How to turn (A, B, C) into (AB, AC, BC) with Pig?
  • Flash radiobutton: how do I get the selected radiobutton?
  • Set focus to first invalid form element in AngularJS
  • How can I extract results of aggregate queries in slick?
  • Sort List of Strings By Version
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • How to suppress a dialog
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Is there any way to access browser form field suggestions from JavaScript?
  • Convert array of 8 bytes to signed long in C++
  • Hazelcast - OperationTimeoutException
  • How to set the response of a form post action to a iframe source?
  • Understanding cpu registers
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • Qt: Run a script BEFORE make
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • reshape alternating columns in less time and using less memory