sorting

Sort date in Javascript

社会主义新天地 提交于 2020-01-21 10:07:11
问题 I have retrieve from ajax query a news feed. In this object, there is a date in this format : Wed, 22 May 2013 08:00:00 GMT I would like to sort all objects by date. Is it possible to do this using Javascript ? UPDATE Using this piece of code it works fine ! array.sort(function(a,b){ var c = new Date(a.date); var d = new Date(b.date); return c-d; }); 回答1: 1) You can't sort objects. The order of the object's keys is arbitrary. 2) If you want to sort an array by date (and they are already date

Moving all zeros to the end of the list while leaving False alone

两盒软妹~` 提交于 2020-01-21 09:51:07
问题 Suppose I have a list: [9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9,False] and I want to move all zeros to the end. I know I can use: sorted([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9], key=lambda x: x == 0) but it will move False to the end of the list as well, which is not what I want. How do I move only zeroes but leave False values at their original places? 回答1: Since bool is a subclass of int and False == 0 is True (indeed, the success of our sorted key function depends on this), if

To find minimum elements in array which has sum equals to given value

为君一笑 提交于 2020-01-21 03:05:50
问题 I am trying to find out the minimum elements in array whose sum equals the given input.I tried for few input sum but was able to find only a pair in first case while I need to implement for more than just a pair. var arr = [10, 0, -1, 20, 25, 30]; var sum = 45; var newArr = []; console.log('before sorting = ' + arr); arr.sort(function(a, b) { return a - b; }); console.log('after sorting = ' + arr); var l = 0; var arrSize = arr.length - 1; while (l < arrSize) { if (arr[l] + arr[arrSize] ===

How to sort ArrayList<Object> in Ascending order android

佐手、 提交于 2020-01-21 00:05:17
问题 I have a requirement to sort ArrayList<Object> (Custom objects) in Ascending order based upon name. For that purpose I am using comparator method as like My ArrayList : ArrayList<Model> modelList = new ArrayList<Model>(); Code I am using: Comparator<Model> comparator = new Comparator<Model>() { @Override public int compare(CarsModel lhs, CarsModel rhs) { String left = lhs.getName(); String right = rhs.getName(); return left.compareTo(right); } }; ArrayList<Model> sortedModel = Collections

How to sort ArrayList<Object> in Ascending order android

倾然丶 夕夏残阳落幕 提交于 2020-01-21 00:05:11
问题 I have a requirement to sort ArrayList<Object> (Custom objects) in Ascending order based upon name. For that purpose I am using comparator method as like My ArrayList : ArrayList<Model> modelList = new ArrayList<Model>(); Code I am using: Comparator<Model> comparator = new Comparator<Model>() { @Override public int compare(CarsModel lhs, CarsModel rhs) { String left = lhs.getName(); String right = rhs.getName(); return left.compareTo(right); } }; ArrayList<Model> sortedModel = Collections

Is there C# support for an index-based sort?

穿精又带淫゛_ 提交于 2020-01-20 05:55:30
问题 Is there any built-in C# support for doing an index sort? More Details: I have several sets of data stored in individual generic Lists of double. These are lists always equal in length, and hold corresponding data items, but these lists come and go dynamically, so I can't just store corresponding data items in a class or struct cleanly. (I'm also dealing with some legacy issues.) I need to be able to sort these keyed from any one of the data sets. My thought of the best way to do this is to

Sorting std::unordered_map by key

耗尽温柔 提交于 2020-01-20 03:55:26
问题 How can I sort an unordered_map by key? I need to print an unordered_map sorted by key. 回答1: std::unordered_map<int, int> unordered; std::map<int, int> ordered(unordered.begin(), unordered.end()); for(auto it = ordered.begin(); it != ordered.end(); ++it) std::cout << it->second; 回答2: An alternate solution is to construct a vector of the keys, sort the vector, and print per that sorted vector. This will be considerably faster than the approaches that constructed a map from the ordered map, but

Are the sorting algorithms used by NSArray stable sorts?

心不动则不痛 提交于 2020-01-19 16:11:44
问题 Are the sorting algorithms used by the various sorting methods in NSArray stable? (As in are they "stable sort" algorithms, where items with the same sort key have their relative orders preserved.) 回答1: Stable sort is not guaranteed unless you use NSSortStable . From the documentation on NSSortOptions: NSSortStable Specifies that the sorted results should return compared items have equal value in the order they occurred originally. If this option is unspecified equal objects may, or may not,

Are the sorting algorithms used by NSArray stable sorts?

隐身守侯 提交于 2020-01-19 16:10:14
问题 Are the sorting algorithms used by the various sorting methods in NSArray stable? (As in are they "stable sort" algorithms, where items with the same sort key have their relative orders preserved.) 回答1: Stable sort is not guaranteed unless you use NSSortStable . From the documentation on NSSortOptions: NSSortStable Specifies that the sorted results should return compared items have equal value in the order they occurred originally. If this option is unspecified equal objects may, or may not,

VBScript Coding Challenge: Sort Array by String Length after Split Command

冷暖自知 提交于 2020-01-19 06:22:08
问题 I am a C student in VBScript coding and need a little help. My code executes a split command as follows: outputArray = split(Description," ") With the individual words from Description now in an array, I want to sort the array based on the string length for each word. So, for example, if Description is equal to "this is an example of a description" then my array values are [this, is, an, example, of, a, description], right? But I want to resort the array so that the longest words are first, i