This Script Wont Sort Correctly by Age

How to sort in bash script?

add this line

sort -o $INPUT -n -t , -k4.7,4 -k4.4,4.5 -k4.1,4.2 $INPUT

After

INPUT=./Birthdays.csv

Google Script sort troubleshooting (works as 2 functions but not combined)

You can try adding the flush() method in between the two actions.

flush()

Applies all pending Spreadsheet changes.

Spreadsheet operations are sometimes bundled together to improve
performance, such as when doing multiple calls to Range.getValue().
However, sometimes you may want to make sure that all pending changes
are made right away, for instance to show users data as a script is
executing.

function SortList() {

var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange("A7:D299");

// Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

var copyrange = sheet.getRange("A6:C6");
var pasterange = sheet.getRange("A298:C298");
var copyvalues = copyrange.getValues();

pasterange.setValues(copyvalues);
copyrange.clearContent();

//Flush
SpreadsheetApp.flush();

// Sorts by the values in the first column (A)
range.sort({column: 4, ascending: false});

}

Sort spreadsheet values based on date column does not work - only sort alphabetically is offered

Posting this for documentation purposes.

As already mentioned, not all values in F are dates. That's most likely related to the fact that month and day are switched when compared to the values in E. Because of this, dates where day is higher than 12 are not valid (since there are only 12 months), and hence become text. And even in valid dates, the date in F and the "date" (actually text) in E have month and day switched up (e.g. 1 June becomes 6 January and vice versa).

I don't know how the dates in F were imported, but consider trying to do it again, taking into account this problem.

How can I sort data by age values?

I think there is a misunderstanding of how objects work in JavaScript. Objects have a set of properties. Each property may have a value.

Properties don't have any particular order.

Think about it this way.

let x = {};
x.foo = 'hello';
y.bar = 'whats up?';
x.foo = 'world';

What order are the keys in? See, the question does not really make sense.

When you express an object literal like you did, you aren't implying any particular order.

var date = {
michael : { color : "A", age : 30 },
peter : { color : "C", age : 10 },
martin : { color : "D", age : 7 },
mario : { color : "R", age : 20 },
};

Is identical to

date.michael = { color : "A", age : 30 };
date.peter = { color : "C", age : 10 };
date.martin = { color : "D", age : 7 };
date.mario = { color : "R", age : 20 };

Maybe put more concisely, the order in which the lines in your source code appear does not really affect the object. It's like saying it has michael and peter, not, a list (michael, peter).

So what you are asking, in the most literal sense, is not possible. You can't sort an object's keys because they have no order any way. It's like sorting a car.

Now, if this were an array of objects, and each object had a property called name, this would be easy. Why? Arrays have an order. Arrays are by definition an ordered list of values.

var people = [
{ name: "michael", color : "A", age : 30 },
{ name: "peter", color : "C", age : 10 },
{ name: "martin", color : "D", age : 7 },
{ name: "mario", color : "R", age : 20 },
];

people.sort((a, b) => b.age - a.age);

will result in

[
{ name: "martin", color : "D", age : 7 },
{ name: "peter", color : "C", age : 10 },
{ name: "mario", color : "R", age : 20 },
{ name: "michael", color : "A", age : 30 },
]

Detailed documentation on Array.prototype.sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Terminal: SORT command; how to sort correctly?

If I understand the problem correctly, you want the "natural sort order" as described in Natural sort order - Wikipedia, Sorting for Humans : Natural Sort Order, and macos - How does finder sort folders when they contain digits and characters?.

Using Linux sort(1) you need the -V (--version-sort) option for "natural" sort. You also need the -f (--ignore-case) option to disregard the case of letters. So, assuming that the file names are stored one-per-line in a file called files.txt you can produce a list (mostly) sorted in the way that you want with:

sort -Vf files.txt

However, sort -Vf sorts underscores after digits and letters on my system. I've tried using different locales (see How to set locale in the current terminal's session?), but with no success. I can't see a way to change this with sort options (but I may be missing something).

The characters . and ~ seem to consistently sort before numbers and letters with sort -V. A possible hack to work around the problem is to swap underscore with one of them, sort, and then swap again. For example:

tr '_~' '~_' <files.txt | LC_ALL=C sort -Vf |  tr '_~' '~_'

seems to do what you want on my system. I've explicitly set the locale for the sort command with LC_ALL=C ... so it should behave the same on other systems. (See Why doesn't sort sort the same on every machine?.)

Sort() with non-existent values

Your default sort solution is set to keep the item in its current position --> return 0. You could provide another conditional that captures undefined and return -1

const
names = [{ name: "John", age: 27 }, { name: "Charles" }, { name: "Ellen", age: 30 }, { name: "Mario" }, { name: "Emanuelle", age: 18 }];

names.sort(function (a, b) {
if(b.age === undefined) return -1;
if (a.age > b.age) return 1;
if (a.age < b.age) return -1;
return 0;
})

console.log(names) // Sort not working, prints original order

tablesorter not sorting updated rows correctly

Your code works in JSFiddle, but not on your page because the javascript is run before the <body> has completed loading. Either move all your javascript to the bottom of the page, above the </body> tag, or wrap the javascript in a document ready function:

$(function() {
// add all your javascript here
});

Edit: Oh and the parser name is "digit" not "integer":

{ 4: {sorter:"digit"} }

Edit2: Your fiddle is loading tablesorter twice and initializing it a bunch of times inside the for loop - here is an updated demo and the fixed loops code:

function populateTable() {

var tableRef = document.getElementById('players').getElementsByTagName('tbody')[0];
for (var i = 1; i < totalPlayers; i++) {
var row = tableRef.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = playernamelink[i];
cell2.innerHTML = playerclass[i];
cell3.innerHTML = playerspec[i];
cell4.innerHTML = playerlvl[i];
cell5.innerHTML = '0';
}
//initialize tablesorter
$("#players").tablesorter({
headers: {
4: {
sorter: "digit"
}
}
});
}

Update 2: Oh, I missed the fact that more asynchronous data was being loaded within the ajax complete callback. I think the easiest solution would be to set up some promises and then in the done function, update tablesorter (updated demo):

var items = [];
function getIlvl(realm, player, index) {
var ilvl;
items.push($.ajax({...}));
// ...
}

function populateTable() {
// ...
$.when.apply($, items).done(function() {
$('#players').trigger('update');
});
}

How to sort sheets within a spreadsheet in chronological order by date in google apps script

  • You want to sort the sheets in a Spreadsheet.
  • The format of sheet name is MM-dd-yyyy.
  • There is a sheet with the sheet name of GUI.
  • There is several sheets with the sheet name of MM-dd-yyyy.
  • You want to sort the sheets as follows.

    • The 1st sheet is GUI. The sheets of MM-dd-yyyy are from earliest date to latest date.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

In this modification, I used the following flow.

  1. Retrieve all sheets.
  2. Create an object array for sorting.

    • The object includes the sheet object and the value converted from MM-dd-yyyy to the unix time.
  3. Sort the object array with the converted values.
  4. Rearrange the sheets using the sorted array.

Modified script:

function sortSheets () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var obj = sheets.map(function(s) {
var sheetName = s.getSheetName();
var date = 0;
if (sheetName != "GUI") {
var ar = sheetName.split("-");
date = new Date(ar[2], ar[0], ar[1]).getTime();
}
return {sheet: s, converted: date};
});
obj.sort(function(a, b) {return a.converted > b.converted ? 1 : -1});
obj.forEach(function(s, i) {
ss.setActiveSheet(s.sheet);
ss.moveActiveSheet(i + 1);
});
}

References:

  • Date
  • map()

If I misunderstood your question and this was not the result you want, I apologize.

Edit:

From your shared Spreadsheet, it was found that the format of the sheet name is not MM-dd-yyyy. That was MM/dd/yyyy. In this case, please modify above script as follows.

From:

var ar = sheetName.split("-");

To:

var ar = sheetName.split("/");

default sort not working in v-data-table Vuetify

The best way to keep it safe is to set sort-by in your data-table:

<v-data-table
:sort-by="['age']"
:headers="headers"
:items="users"
:disable-pagination="true"
v-model="users"
hide-default-footer
class="elevation-1"
>

[UPDATE]

To complete Václav Procházka's answer, you can also provide sort-desc to force it to be decrescent.

Also It's an anti-pattern since v2 to mutate props directly check docs here.
The ideal way is to make a clone of it or to use a computed property like below.

Using this.$emit('update:usersProp', val) will try to update your prop on parent component so you parent should look like this <Users :usersProp.sync="users"></Users> or if you use this.value and this.$emit(input, val), then you can use <Users v-model="users"></Users>. Docs here and here



new Vue({
el: '#app',
vuetify: new Vuetify(),
props: {
usersProp: {
default: () => {
return [
{full_name: 'Stacey Purkis', city: 'Calgary', age: 32 },
{full_name: 'Jazz Alzo', city: 'London', age: 24 },
{full_name: 'James Ross', city: 'Toronto', age: 45 }
]
}
}
},
data: () => ({
headers: [
{ text: 'Name', value: 'full_name', sortable: false },
{ text: 'Address', value: 'address', sortable: false },
{ text: 'Age',value: 'age' }
],
}),
computed: {
users: {
get () {
return this.usersProp
},
set (val) {
this.$emit('update:usersProp', val)
}
}
}
})
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app>
<v-main>
<v-container>
<v-data-table
:headers="headers"
:items="users"
:sort-by="['age']"
:sort-desc="true"
:disable-pagination="true"
v-model="users"
hide-default-footer
class="elevation-1"
></v-data-table>
</v-container>
</v-main>
</v-app>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
</body>
</html>


Related Topics



Leave a reply



Submit