0

I have a problem sorting an array. I am not the smartest concerning these sort algorithms. The array should have following structure:

 var arr = [
 [week, IssuesPriority1, IssuesPriority2, IssuesPriority3],
 [week, IssuesPriority1, IssuesPriority2, IssuesPriority3],
 [week, IssuesPriority1, IssuesPriority2, IssuesPriority3],
 ...
 ];

So for each week there is a number of issues for the priority very high, high, medium. The string that needs to be parsed in this structure is following:

 var string =
 "26|3|1,27|6|1,28|7|1,29|2|1,30|2|1,31|2|1,32|2|1,33|3|1,
  35|1|1,34|2|1,36|0|1,37|0|1,38|1|1,26|11|2,27|10|2,28|9|2,
  29|13|2,30|10|2,31|8|2,32|10|2,33|12|2,34|14|2,35|11|2,
  36|11|2,37|12|2,38|14|2,27|17|3,26|13|3,29|26|3,28|21|3,30|25|3,
  31|20|3,34|30|3,32|18|3,33|25|3,35|33|3,36|28|3,38|28|3,37|27|3";

  var arr = string.split(",");

  for(var i = 0; i < arr.length; i++){
      var currentArr = arr[i].split("|");
      var week = currentArr[0];
      var issues = currentArr[1];
      var priority = currentArr[2];
  }

I have a lack of ideas sorting it in the desired way. Can you help me?

sanyooh
  • 1,260
  • 2
  • 18
  • 31
  • Is the `week` your sorting comparator? – Tom A Sep 25 '14 at 15:13
  • First build the array, then `.sort()` it. What's your problem with sorting it? What should the ordering look like, in which "special manner" do you want to have it ordered? – Bergi Sep 25 '14 at 15:16
  • Yes, to each week belong 3 issue values. – sanyooh Sep 25 '14 at 15:17
  • @user3328233 That doesn't tell how these values should be sorted... should we sort the entries by week? By the count of issue priorities? By the highest priorities? ... – plalx Sep 25 '14 at 15:19
  • When the array is sorted it should look like: [ [26,3,11,13], [27,6,10,17], [28,7,9,21],...] so for each week: on index 0 - week, index 1 - issues with priority 1, index 2 - issues with priority 2, index 3 - issues with priority 3 – sanyooh Sep 25 '14 at 15:21
  • consider using something similar to: http://stackoverflow.com/questions/2466356/javascript-object-list-sorting-by-object-property – Adrian B. Sep 25 '14 at 15:30

2 Answers2

0

I don't think you want any sorting at all. You are looking for grouping!

var arr = string.split(",");
var weeks = {};
for (var i = 0; i < arr.length; i++) {
    var currentArr = arr[i].split("|");
    var week = currentArr[0];
    var issue = currentArr[1];
    var priority = currentArr[2];
    if (!(week in weeks))
        weeks[week] = {1:[], 2:[], 3:[]};
    // if the number of issues levels were unknown,
    // you'd start with an empty object instead
    // and create the arrays dynamically in a manner similar to the weeks
    weeks[week][priority].push(issue);
}
return Object.keys(weeks).map(function(week) {
    return [week, weeks[week][1], weeks[week][2], weeks[week][3]];
});

(to get the result ordered by week number, add a sort(function(a,b){return a-b}) before the .map() call)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

In your situation I would recommand to put the values in the array first. In the second step I would sort the array using the sort method.

function getSortedArrayByString(myString) {
    var arraySplittedString, i, tmpValueArray, tmpInnerArray, resultingArray;

    arraySplittedString = myString.split(",");
    resultingArray = [];

    for(i = 0; i < arraySplittedString.length; i++){
        // tmpArray has the format of [<week>, <IssuesPriority1>, <IssuesPriority2>]
        tmpValueArray = arraySplittedString[i].split("|");
        // Push it in the new array.
        resultingArray.push(tmpValueArray);
    }

    // Sort array by weeks ascending. 
    resultingArray.sort( function (a, b) {
        return a[0] - b[0];
    });

    return resultingArray;
}

Running fiddle.

If you also want to sort by the count of issues, you simply can customize the inner sort function.

With this solution all values are saved as strings. You can convert them by using the parseInt function.

Marvin
  • 539
  • 5
  • 17