4

I have two arrays. The first is for strings and the second is an array for their number of occurrences.

I am trying to get the top 10 most occurrences of words in the first array. I sorted it but somehow I only sort it alphabetically and the second array respectively corresponds to their number of occurrences.

How can I sort the second array from biggest to lowest and, at the same time, sort the first array that match the order of the second?

I'm having trouble inserting my json to my highcharts and I found out why, the numbers should be in square brackets [] I tried already inserting [] in 1 but still does not work please see my post I edit it

this is the data that i should insert in the highchart

[{"name":"#murrayftw","data":[46]},
 {"name":"#job","data":[37]},
 {"name":"#jobs","data":[25]},
 {"name":"#favnashvine","data":[16]},
 {"name":"#rollersmusicawards","data":[14]},
 {"name":"#soa","data":[13]},
 {"name":"#tweetmyjobs","data":[12]},
 {"name":"#sman1boyolangu","data":[12]},
 {"name":"#supernatural200thepisode","data":[12]},
 {"name":"#veteransday","data":[12]}]
J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • 1
    Do you have a sample data set to work with? – Nick Tomlin Nov 25 '14 at 00:27
  • Instead of using two unrelated arrays, use an object that keeps a reference to the word, and the count of that word `[{'word' : 'hello', 'count' : 2}, {'word' : 'world', 'count' : 1}]`, and sort that array based on the counts of the words. – David Thomas Nov 25 '14 at 00:28
  • that is exactly what i'm going to do next to use an object.. so if i put it in an object and sort the array it will also sort the words? –  Nov 25 '14 at 00:31

4 Answers4

1

Try using a custom compare function with your .sort() call! Check out the documentation here.

I would, in this example (probably not the best way):

  1. Have the unsorted "count" array
  2. Have the unsorted word array
  3. Sort the word array (with a custom function described below)
  4. Sort the count array (no custom function)

The custom compare function would probably be a simple lookup and return the corresponding value in the unsorted count array. (i.e. if the word "a" is 0th index and its relevant count amount is in the 0th index of the count array, return count[0])

alex
  • 479,566
  • 201
  • 878
  • 984
Vikram Saran
  • 1,143
  • 8
  • 17
1

If you cannot work with an object try using nested for loops:

var array1 = ['z', 'd', 'e', 'f', 't'], arr1Count = array1.length;
var array2 = [1, 12, 5, 7, 3];
var sortedArray2 = array2.sort(function(x, y) {return y - x});

var i, j, sortedArray1 = [];

for (i = 0; i < arr1Count; i++) {
    for (j = 0; j < arr1Count; j++) {
        if (array2[j] === sortedArray2[i]) sortedArray1.push(array1[j]); //iterate through the unsorted numeric array (array2) and when it matches the sortedArray2, push this index of array1 into the sortedArray1
    }
}
0

This will create an array of objects that are then sorted by count.

var hashtags = {},
    counts = [];

for (var i in data)
{
    if(data[i].lang == "en")
    {   
        for (var j in data[i].entities.hashtags)
        {
            var text = data[i].entities.hashtags[j].text;

            if(text) {
                if(hashtags[text]) {
                     hashtags[text].data[0]++;
                } else {
                     hashtags[text] = {
                         name: text,
                         data: [1]
                     };
                     counts.push(hashtags[text]);
                }
            }
        }
    }
}

counts.sort(function(a, b) { return b.data[0] - a.data[0]; });
rdubya
  • 2,916
  • 1
  • 16
  • 20
  • thank you very much for this answer it worked fine for me but i will just ask one more time i have get this object {"FrozenOnIce":{"text":"FrozenOnIce","count":1},"bestbdaygift":{"text":"bestbdaygift","count":1}} I only want to get {"text":"FrozenOnIce","count":1},{"text":"bestbdaygift","count":1}} how do i do that? –  Nov 25 '14 at 01:22
  • the `hashtags` object is just used as a lookup table to make it easier to find already seen words. You want to use the `counts` array. It contains what you want in the format you want it. – rdubya Nov 25 '14 at 01:32
  • thank you sir! by the way im having trouble inserting my json to my highcharts and i found out why, the numbers should be in square brackets [] i tried already inserting [] in 1 but still does not work please see my post i edit it –  Nov 25 '14 at 02:52
  • I've updated my answer to return what you need. What kind of chart are you trying to create? – rdubya Nov 25 '14 at 09:26
  • it is a poll chart sir.. thank so much again for answering my question! im kinda stuck on that problem im very glad for your reply thank again :D –  Nov 25 '14 at 10:55
0

Simple - don't use 2 arrays but one collection which every element is an object

I took the basics from this post: Sorting JavaScript Object by property value

and completed the demo:

var collection = {car:300, bike:60, motorbike:200, airplane:1000, helicopter:400, rocket:8*60*60}
var sortable = [];
for (var item in collection)
      sortable.push([item, collection[item]])
sortable.sort(function(a, b) {return a[1] - b[1]})

collection = {};
for (var i in sortable)
{
    collection[sortable[i][0]] = sortable[i][1];
}

console.log(collection);
Community
  • 1
  • 1
ymz
  • 6,602
  • 1
  • 20
  • 39