0

I have large data structure to manipulate and I have some problems sorting similar data into sub array.

var arr = [2016-12-16, 2016-12-16, 2016-12-17, 2016-12-17, 2016-12-17, 2016-12-17, 2016-12-18, 2016-12-18, 2016-12-19]; 

I wanna sort like this.

[[2016-12-16, 2016-12-16], [ 2016-12-17, 2016-12-17, 2016-12-17, 2016-12-17],[2016-12-18, 2016-12-18] [2016-12-19]] 
2
  • 2
    Are those elements (dates?) supposed to be strings? Right now you're subtracting numbers. Commented Dec 16, 2016 at 17:50
  • So you want to sort them by day? Also, to agree with @qxz, I am reasonably certain that you need to make sure those are strings because otherwise you're doing the math of something like 2016-12-16 which is 1988 Commented Dec 16, 2016 at 17:54

3 Answers 3

1

Array#reduce the array into an array of arrays by using a object as a hash:

var arr = ['2016-12-16', '2016-12-16', '2016-12-17', '2016-12-17', '2016-12-17', '2016-12-17', '2016-12-18', '2016-12-18', '2016-12-19'];

var result = arr.reduce(function(r, item) {    
  (r.hash[item] || (r.hash[item] = r.arr[r.arr.push([]) - 1])).push(item);
  
  return r;
}, { arr: [], hash: {} }).arr;

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way to do this is with a dictionary (which JS objects naturally act as).

var datesArray = [ "2016-12-16", "2016-12-16", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-18", "2016-12-18", "2016-12-19" ]; //I've corrected the syntax for this to make each date a string, rather than a sequence of subtractions

var datesCount = {};
datesArray.forEach(function(date){
    if(!datesCount[date])
        datesCount[date] = []
    datesCount[date].push(date);
});

At this point, you have a set of group arrays, each containing every instance of a particular date. You can iterate over it with Object.getOwnPropertyNames and put each array into the larger array OR you could just use it directly.

Comments

0

try to divide the code to sub problems-

  1. first, convert all element to strings (before writing a function).
  2. copy the array to manipulate without changing original.
  3. sort elements.
  4. identify repeated elements.
  5. create sub arrays for repeated elements.

this a code I've written for a differend purpose and modified it for the explanation. there might be more effient ways to write it, i'm still learning..

const dateStringSort = (array) => {
//assign to a new array - wont change exist array
    const arr = [].concat(array);
//sort elements
    arr.sort();
//identify repeated elements and group them to sub arrays.
    const arr1 = []; //assign another array to prevent inifinty loops
    while (arr.length>0){
        let i = arr[0];
        let filtered = arr.filter(item=>item===i);//return new array with all reapeted elements
        arr1.push(filtered); //push the filtered array to new array as a sub array
        arr.splice(0,(filtered.length)); //delete all the repeated elements from arr to prevent double sub araays
    };
//return new array
return arr1;
};

//run function
const originArray = ["2016-12-16", "2016-12-16", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-18", "2016-12-18", "2016-12-19"]; 
const newArray = dateStringSort(originArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.