Posts

Showing posts with the label sorting

How can I find closest date value from an array according to the current date?

How can I find closest date value from an array according to the current date? I want to get the next closest date on this array according to the current date. var dates = [ 'Aug 18, 2018 03:24:00', 'August 19, 2018 03:24:00', 'September 17, 2018 03:24:00', 'September 14, 2018 03:24:00', 'August 18, 2018 03:24:00', 'July 16, 2018 03:24:00', 'July 15, 2018 03:24:00', 'December 15, 2018 03:24:00', 'July 13, 2018 03:24:00', ]; var now = new Date(); 4 Answers 4 First you need to convert each date into a timestamp then subtract each of them by the current date and store the timestamp difference in the temp array then get the index of the minimum value and use the index to access the closest date in the original array. var dates = [ 'July 16, 1995 03:24:00', 'Aug 18, 1995 03:...

Grouping neighbouring entries with the same value in a 2d array

Grouping neighbouring entries with the same value in a 2d array EDIT: I now realized the question was not appropriate for stack but I've gotten a lot of helpful advice anyway. Thanks everyone! I have a 2d array and I want to group together neighbors of the same value. Using C# (working with unity). Let's say I have this: int[,] array { 0,0,0,0,0,0,1,0,0,0, 0,1,1,0,0,0,1,0,0,0, 0,1,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,1,0, 0,0,0,0,0,0,1,1,1,0 } There are three "clusters" of 1:s. I want to add them to a dictionary with some variable for identification. So maybe first add the neighboring values to a list, add that list to a dictionary, clear the list and move onto the next cluster. The columns and rows would be of equal length in the real thing. I would also want the sorting method to accept arrays of various sizes so no hardcoded values. I parse the array from an XML document. I've tried looking into Array.Sort but the resources I have fou...

How to sort data coming from the API by date in Angular 4

How to sort data coming from the API by date in Angular 4 This is the JSON data i am fetching from POSTMAN. I want it to be ordered in a nearest to todays date. I tried many angular pipes but unfortunately nothings working. Any help would be great. I want to sort the date by the "broadcastOn" field. Thanks in advance. [ { "messageId": "09ca0609-bde7-4360-9d3f-04d6878f874c", "broadcastOn": "2018-02-08T11:06:05.000Z", "message": "{"title":"Server Side Test 2","text":"Different Message again","image":"https://api.adorable.io/avatars/285/abott@adorable.png","url":"https://www.google.co.in"}" }, { "messageId": "0a5b4d0c-051e-4955-bd33-4d40c65ce8f7", "broadcastOn": "2018-02-08T10:36:27.000Z", "message": "{"title":"Broadc...

Sort a list of Comparables using multiple string fields in a custom order

Sort a list of Comparables using multiple string fields in a custom order With this class: public class MyClass implements Comparable<MyClass> { private String status; private String name; private String firstName; @Override public int compareTo(MyClass o) { return 0; } } I'd like to sort a list of MyClass objects with this order: How can I do this with the Comparable interface ? Comparable Well, what have you tried so far? – Laurens Jun 29 at 9:29 stackoverflow.com/questions/369512/… I tried the third answer in this post assuming I'm not using java 8. My filters are not the same, I don't know how to check by "keys" – whySoSerious Jun 29 at 9:32 ...

Sorting a vector of vectors by multiple elements in Julia

Sorting a vector of vectors by multiple elements in Julia I've had good read of the 'Sorting Functions' section of the Julia manual, and had a look at some of the similar questions that have already been asked on this board, but I don't think I've quite found the answer to my question. Apologies if I've missed something. Essentially I have a vector of vectors, with the enclosed vectors containing integers. For the purposes of the example, each enclosed vector contains 3 integers, but it could be any number. I want to sort the enclosed vectors by the first element, then by the second element, then by the third element etc. Let's start with the vector: v = [[3, 6, 1], [2, 2, 6], [1, 5, 9], [2, 1, 8], [3, 7, 9], [1, 1, 2], [2, 2, 2], [3, 6, 2], [1, 2, 5], [1, 5, 6], [3, 7, 4], [2, 1, 4], [2, 2, 1], [3, 1, 2], [1, 2, 8]] And continue with what I'm actually looking for: v = [[1, 1, 2], [1, 2, 5], [1, 2, 8], [1, 5, 6], [1, 5, 9], [2, 1, 4], [2, 1, ...

Quicksort implementation program array out of bounds error and not sorting generated numbers

Quicksort implementation program array out of bounds error and not sorting generated numbers I am new to programming and cannot seem to figure out my errors when trying to write a Quicksort program. I have most of the implementation done with the exception of a few errors that I cannot seem to figure out. Here is my current code: public class Quicksort { public static void main(String args) { Integer numbers = new Integer[20]; for (int i = 0; i < numbers.length; i++) { numbers[i] = (int) (Math.random() * 100); } printArray(numbers); quicksort(numbers); printArray(numbers); } public static <T> void printArray(T array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + (i < array.length - 1 ? ", " : "n")); } } public static <T> void printArrayPartition(T array, int low, int high, int pivot) { for (int i = 0; i ...