logo
down
shadow

Sorting a list based on instructions in Javascript


Sorting a list based on instructions in Javascript

Content Index :

Sorting a list based on instructions in Javascript
Tag : javascript , By : KingGuppy
Date : January 11 2021, 03:32 PM

like below fixes the issue AuxTaco had the right idea! It is a topological sort!
Since I don't care about implementing a topological sort, I simply use the one from NPM, specifically @hapi/topo.
const Topo = require('@hapi/topo');

let list = new Topo()
let counter= 0
list.add('one', {group:'one'}) //this package requires adding the group name so we make it the same
list.add('four', {group: 'four', after: 'one', sort: counter++})
list.add('three', {group:'three', before: 'four', after:'two', sort: counter++})
list.add('two', {group: 'two', after: 'one', sort: counter++})

list.nodes
//returns ['one', 'two', 'three', 'four']

//example from Asthmatic's comment


list = new Topo()
counter = 0
list.add('one', {group:'one', sort: counter++}) //this package requires adding the group name so we make it the same
list.add('four', {group: 'four', after: 'one', sort: counter++})
list.add('two', {group: 'two', after: 'one', sort: counter++})

list.nodes
// returns ['one', 'four', 'two']

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

sorting multiple lists in python based on sorting of a single list


Tag : python , By : Chandra P Singh
Date : March 29 2020, 07:55 AM
help you fix your problem I am a python newbie here, and I have been struck on a rather simple problem - and I am looking for the most efficient way to solve this. So, I have 5 lists as follows:
sorted_lists = sorted(izip(a, b, c, d, score), reverse=True, key=lambda x: x[4])
a, b, c, d, score = [[x[i] for x in sorted_lists] for i in range(5)]
a, b, c, d, score = izip(*sorted(izip(a, b, c, d, score), reverse=True,
                         key=lambda x: x[4]))
def sort_lists_by(lists, key_list=0, desc=False):
    return izip(*sorted(izip(*lists), reverse=desc,
                 key=lambda x: x[key_list]))

Javascript: custom array sorting or sorting based on a pattern


Tag : javascript , By : Mark W
Date : March 29 2020, 07:55 AM
With these it helps The callback function doesn't sort itself. It just needs to compare any two items that are passed to it. So you have to write the logic that translates strings starting with 'translate' come before strings starting with 'rotate'.
// Very simple, rudimentary function to translate a type to number. Improve at will.
function typeIndex(x) {
  if (x.indexOf('translate') > -1) return 0;
  if (x.indexOf('rotate') > -1) return 1;
  if (x.indexOf('skew') > -1) return 2;
  if (x.indexOf('scale') > -1) return 3;
  return 1000; // Unknown
}

var p = ['skewX', 'rotateY', 'rotateZ', 'translateY', 'scale', 'rotateX', 'ordered skewing'];


// Sort array using callback;
p.sort(function(a, b){
  // First compare the difference based on type.
  var result = typeIndex(a) - typeIndex(b);
  
  // If the difference is 0, they are of the same type. Compare the whole string.
  if (result == 0) 
    result = a.localeCompare(b);
  
  return result;
});

console.log(p);

Sorting words in a list of strings based on their relative frequencies, not regular sorting?


Tag : python , By : Eniac
Date : March 29 2020, 07:55 AM
This might help you Python 2
All you have to do is create custom comparator based on your counter and call sorting
s = ["hello there you would like to sort me", 
    "sorted i would like to be", "the banana does not taste like the orange", 
    "my friend said hello", "hello there amigo", "apple apple banana orange peach pear plum", 
    "orange is my favorite color"]


from collections import Counter

def create_word_freq_dict(series):
    return Counter(word for row in series for word in row.lower().split())

word_counts = create_word_freq_dict(s)

for row in s:
    print sorted(row.lower().split(), lambda x, y: word_counts[y] - word_counts[x])
['hello', 'like', 'there', 'would', 'to', 'you', 'sort', 'me']
['like', 'would', 'to', 'sorted', 'i', 'be']
['like', 'orange', 'the', 'banana', 'the', 'does', 'not', 'taste']
['hello', 'my', 'friend', 'said']
['hello', 'there', 'amigo']
['orange', 'apple', 'apple', 'banana', 'peach', 'pear', 'plum']
['orange', 'my', 'is', 'favorite', 'color']
for row in s:
    sorted_row = sorted(row.split(), lambda x, y: word_counts[y] - word_counts[x])
    print zip(sorted_row, map(lambda x: word_counts[x], sorted_row))
[('hello', 3), ('like', 3), ('there', 2), ('would', 2), ('to', 2), ('you', 1), ('sort', 1), ('me', 1)]
[('like', 3), ('would', 2), ('to', 2), ('sorted', 1), ('i', 1), ('be', 1)]
[('like', 3), ('orange', 3), ('the', 2), ('banana', 2), ('the', 2), ('does', 1), ('not', 1), ('taste', 1)]
[('hello', 3), ('my', 2), ('friend', 1), ('said', 1)]
[('hello', 3), ('there', 2), ('amigo', 1)]
[('orange', 3), ('apple', 2), ('apple', 2), ('banana', 2), ('peach', 1), ('pear', 1), ('plum', 1)]
[('orange', 3), ('my', 2), ('is', 1), ('favorite', 1), ('color', 1)]
s = ["hello there you would like to sort me", 
    "sorted i would like to be", "the banana does not taste like the orange", 
    "my friend said hello", "hello there amigo", "apple apple banana orange peach pear plum", 
    "orange is my favorite color"]

from functools import cmp_to_key
from collections import Counter

def create_word_freq_dict(series):
    return Counter(word for row in series for word in row.lower().split())

word_counts = create_word_freq_dict(s)


for row in s:
    sorted_row = sorted(row.split(), key=cmp_to_key(lambda x, y: word_counts[y] - word_counts[x]))
    print(sorted_row)

Sorting a dictionary which has values as list, and sorting based on the elements inside this list


Tag : python , By : wiznick
Date : March 29 2020, 07:55 AM

Create instructions list based on a diff of two lists


Tag : java , By : Steve O.
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Trying to define a promise.all
  • Python GEE to extract featurecollection timeseries from Landsat imageries
  • How to write negative binary number?
  • Unable to set property of individual object
  • Why Named Function Expression itself cannot assign Name to another Value?
  • How to reset scroll position on single page pagination
  • How to scroll to bottom of page, when a new message is sent - Socket.io
  • How to reconstruct audio blob from a base64 encoded String?
  • How to calculate numbers within an array of objects
  • Using JavaScript to change CSS properties of SVG circles for all circles that was not clicked
  • Is there value in propTypeing repeat required properties at every level of nested components?
  • Call function if variable does not exist in a filter
  • localStorage value doesn't get updated automatically
  • React not updating state with setState correctly inside promises
  • Direct native JavaScript or jQuery method to get containing block of an element
  • Dynamic JSON object to Html Table
  • How to send Id and file to same controller?
  • How to Make a jQuery Table Cell(td) clickable to Run a Function?
  • Prevent the duplicate function onchange event jQuery
  • Reactjs Input Upload doesn't return local URL
  • An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected
  • How to filter a JSON Array, with an eventListener input value at keyup
  • How to separate the different properties of filter in css?
  • Why dont work change event of bootstrap input spinner?
  • How to hide overflow text content in react
  • Is there another way to write line 6
  • Cypress - how to find by text content?
  • How to setstate for the dynamic checkboxes in a loop in react?
  • Is display:none safe?
  • How to create a form to change part of a href url of a button?
  • Put many function inside function in Javascript
  • how to exit from loop after a ajax call
  • Do Angular Reactive Form Validator.min/max check Perform Type Conversions before checking the value?
  • How to Replace Text Within Script Tags Using JQuery
  • add variable to object if not undefined using short-hand
  • How can I effectively curve my items inside a list?
  • Add new record Keyboard Shortcut on Kendo Grid Mvc
  • Time and Date running on page
  • Looping through page links puppeteer doesn't return values from the newly loaded page
  • Problem with Navigator.geoLocation.getCurrentPosition not working
  • Trying to change the id of an element with Javascript
  • Embedded Schema Inserting Blank in Meteor using Collection2
  • avoid useEffect to render at first load?
  • Why is this CSS transition only working correctly on the some clicks? How to solve it?
  • Interpolated values does not reflect the latest values in an Angular app
  • Javascript classes define attribute in constructor or outside the class
  • How to block scroll event passing through to iframe until iframe is active?
  • What is the purpose of "constructor" method in javascript?
  • Stencil JS not working with non-default imports
  • Are frameworks misleadingly named for marketing purposes?
  • React <Redirect> after transition not working
  • How to send multiple parameters in a function using EJS
  • React conditional style on custom click
  • Push a block of information to an array X times based on an external variable
  • How do I populate dropdown with names, and values (numeric ids)?
  • How to apply styling to elements created with .insertAdjacentHtml method
  • Positioning key:value of an object
  • Referencing DOM Elements with React Hooks
  • TypeError: Method "getRange" of null when using multiple scripts (Libraries) in Google scripts
  • Why can't I check if an environment variable is undefined?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com