sorting

How to perform sort in js?

故事扮演 提交于 2020-07-08 10:32:20
问题 I have an array like this var temp = [{"rank":3,"name":"Xan"},{"rank":1,"name":"Man"},{"rank":2,"name":"Han"}] I am trying to sort it as follows temp.sort(function(a){ a.rank}) But its n ot working.Can anyone suggest help.Thanks. 回答1: With Array#sort, you need to check the second item as well, for a symetrical value and return a value. var temp = [{ rank: 3, name: "Xan" }, { rank: 1, name: "Man" }, { rank: 2, name: "Han" }]; temp.sort(function(a, b) { return a.rank - b.rank; }); console.log

Order data.table by a character vector of column names

浪尽此生 提交于 2020-07-08 05:27:07
问题 I'd like to order a data.table by a variable holding the name of a column: I've tried every combination of + eval , get and c` without success: I have colVar = "someColumnName" I'd like to apply this to: DT[order(colVar)] 回答1: You can use double brackets for data tables: library(data.table) dtbl <- data.table(x = 1:5, y = 5:1) colVar = "y" dtbl_sorted <- dtbl[order(dtbl[[colVar]])] dtbl_sorted 回答2: data.table has special functions for that matter which will modify your data set by reference

Sort by just Month name and day. Bash

元气小坏坏 提交于 2020-07-07 12:41:40
问题 Hello Trying to sort this list by Month and day. Can not get it staight. Any pointers ? List example: Jul 23 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted /dev/sdc1 (Read-Write, label "FreeAgent GoFlex Drive", NTFS 3.1) Jul 30 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted /dev/sdb1 (Read-Write, label "HP USB FD", NTFS 3.1) Aug 2 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted /dev/sdb1 (Read-Write, label "WINPART", NTFS 3.1) Jul 24 Drive :EXT (Linux) Details :

Sort by just Month name and day. Bash

∥☆過路亽.° 提交于 2020-07-07 12:40:28
问题 Hello Trying to sort this list by Month and day. Can not get it staight. Any pointers ? List example: Jul 23 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted /dev/sdc1 (Read-Write, label "FreeAgent GoFlex Drive", NTFS 3.1) Jul 30 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted /dev/sdb1 (Read-Write, label "HP USB FD", NTFS 3.1) Aug 2 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted /dev/sdb1 (Read-Write, label "WINPART", NTFS 3.1) Jul 24 Drive :EXT (Linux) Details :

Sort alphabetically in Rails

回眸只為那壹抹淺笑 提交于 2020-07-06 12:27:12
问题 How do I sort an array in Rails (alphabetical order). I have tried: sort_by(&:field_name) which that gives me an array with capital letter order and then lower case order. I have tried: array.sort! { |x,y| x.field_name.downcase <=> y.field_name.downcase } Is there any way to solve this? 回答1: You should first downcase every string and then sort like: array = ["john", "Alice", "Joseph", "anna", "Zilhan"] array.sort_by!{ |e| e.downcase } => ["Alice", "anna", "john", "Joseph", "Zilhan"] 回答2: Be

Javascript: Bubble Sort

本小妞迷上赌 提交于 2020-07-05 11:24:10
问题 I have made a bubble sort algorithm (sorta) using JS. It works sometimes, but the problem is that it only iterates through the array once. Here is my code: function bubble(arr) { for (var i = 0; i < arr.length; i++) { if (arr[i] > arr[i + 1]) { var a = arr[i] var b = arr[i + 1] arr[i] = b arr[i + 1] = a } } return arr; } 回答1: You need an inner loop to complete the sort correctly: function bubble(arr) { var len = arr.length; for (var i = 0; i < len ; i++) { for(var j = 0 ; j < len - i - 1; j++

Javascript: Bubble Sort

我只是一个虾纸丫 提交于 2020-07-05 11:24:05
问题 I have made a bubble sort algorithm (sorta) using JS. It works sometimes, but the problem is that it only iterates through the array once. Here is my code: function bubble(arr) { for (var i = 0; i < arr.length; i++) { if (arr[i] > arr[i + 1]) { var a = arr[i] var b = arr[i + 1] arr[i] = b arr[i + 1] = a } } return arr; } 回答1: You need an inner loop to complete the sort correctly: function bubble(arr) { var len = arr.length; for (var i = 0; i < len ; i++) { for(var j = 0 ; j < len - i - 1; j++

jdbcTemplate query() guaranteed to maintain resultset order?

╄→尐↘猪︶ㄣ 提交于 2020-07-05 09:14:20
问题 My question is similar to the one asked here: http://forum.springsource.org/showthread.php?84508-jdbctemplate.query()-sorted-result-set but a clear answer was not provided - an ArrayList does not guarantee order. Basically, I would like to know if the call returned by jdbcTemplate.query() guarantees the order of the resultset and if I can dump it into a LinkedList and pass it along :) Thanks! Edit: I should clarify that the query does include an order by clause, hence my requirements for a

Sort list by given order of indices

爷,独闯天下 提交于 2020-07-05 06:49:04
问题 I have a list of lines read from a file. I need to sort the list by time stamp. I have parsed out the time stamp using regular expressions and place them into a separate list. The indices of the two lists will match. Once I sort the list of time stamps, I can get the order of indices. Is there a way to apply the same order of indices to the original list of lines? The result should be the sorted list of original lines. Example: listofLines = ['log opened 16-Feb-2010 06:37:56 UTC', '06:37:58

Define fixed sort order in JavaScript

我是研究僧i 提交于 2020-07-05 04:32:10
问题 I haven't found something in my research so I thought someone can help me here. My problem is that I want to sort an array of objects which contains a status: { "data":[ { "status":"NEW" }, { "status":"PREP" }, { "status":"CLOS" }, { "status":"END" }, { "status":"ERR" }, { "status":"PAUS" } ] } Now I want to set a fixed sort order like all objects with the status "END" coming first then all objects with the status "PREP" and so on. Is there a way to do that in JavaScript? Thanks in advance :)