views:

78

answers:

2

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?

Thanks!

+1  A: 

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;
    });
});
strager
I'll give it a shot. So basically, I return my category list into the var there. That I get. The rest I'll have to figure out how best to write it in JS so it works, unless you might know that too. For instance, I've not worked with JS filters before, etc.. But I'll try and forge ahead on translating this to js. I had a feeling it was going to look something like this.
Lynn
This pseudocode looks strangely like valid Javascript. My crystal ball says translating will be easy
MooGoo
@Lynn, If you're using a framework, it probably provides map and filter functions. If not, map iterates through the array, replacing elements with the return value of the function. Filter iterates through the array and removes elements for which the function returns false.
strager
Ok, thanks for the explanation @strager (I'm still coming up to speed on some JS obviously) and thanks @MooGoo for eyeballing it and letting me know it was more js than I thought. I'm mostly a PHPer these days, but am trying to get myself more educated in front-end js coding.
Lynn
Ok, I'm going to have to admit defeat in implementing this. How would I get my list of categories from the DB to map against this function and return values to the switch case statement to write out in the order I want?
Lynn
in other words...how could I use a document.write example to see how the function is called?
Lynn
@Lynn, I really suggest you pick up a framework. I hate to admit (because everyone says this) that you should probably check out jQuery. jQuery has `$.map` and `$.filter` for you, IIRC. To get your data into JavaScript, you need to pass it through an HTTP request. You can either do this when the page loads (by injecting a `<script>` dynamically, creating an object/array with the data) or at run time (by using an Ajax request to fetch the data into an object). If you need any more help, perhaps ask in a separate question, as it's getting a bit off-topic at this point.
strager
I'm trying to learn jQuery currently. There's A LOT to learn. And also doing stuff in basic JS too. Crazy. But thanks for the input.
Lynn
+1  A: 

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

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

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

 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!

Lynn