Posts

Showing posts with the label sort

To get a random available port in your *nix-based machine

For example, If I want to get a random port from the 3000-3999 port pool, I would run the following command in the terminal: comm -23 <(seq 3000 3999 | sort) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1

Sort Python dictionaries by key

Even though dictionaries in Python are unordered, we can use this following technique to sort the dictionaries: >>> import collections >>> d = {2:3, 1:89, 4:5, 3:0} >>> od = collections.OrderedDict(sorted(d.items())) >>> od OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)]) And the best thing is we can still using that collection just like our old dictionary: >> od[1] 89 >>> for k, v in od.iteritems(): print k, v 1  89 2  3 3  0 4  5 Note: the only difference is that we use iteritems() instead of items(). But it will be the same in python 3 (items()) References:   http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key

Using jQuery to sort table by a pseudo column

In many cases, the web engine that generates data tables (html) is close-sourced and you cannot sort those tables. It is not because the engine does not have a mechanism to sort data. It's just that its behaviors does not satisfy your need. For example, the PowerSchool customization engine. In PowerSchool, I can output a table of lunch balance information of a student using this template syntax: ~[tlist;gldetail;studentid=~(curstudid);alternatecolor;sortcmd;date, > ,time, > ;]   <td>~(date)</td>   <td>~(time)</td>   <td>~(neteffect)</td>   <td>~(runninglunchtotalbalance.students)</td>   <td colspan="3">~(transdescription)</td> Did you see the >  (greater than) sign? It says that the table will sort by the date and time columns in ascending order. What I want is to make it sort date and time in descending order. So I tried to replace the > with the < (less than) ho...

Sorting select options in alphabet order

Pretty handyyyy: Reference: http://stackoverflow.com/questions/12073270/sorting-options-elements-alphabetically-using-jquery

Sort table of links with Tablesorter jQuery plugin

Assuming you have a table of links (link a portal) in your website like the following: <table class="tablesorter" id="tbl1"> <thead> <tr> <th> Faculty </th> </tr> </thead> <tbody> <tr> <td><a href="#link1">A link</a></td> </tr> <tr> <td><a href="#link2">G Link</a></td> </tr> <tr> <td><a href="#link3">H Link</a></td> </tr> <tr> <td><a href="#link4">B Link</a></td> </tr> <tr> <td><a href="#link5">K Link</a></td> </tr> <tr> <td><a href="#link6">U Link</a></td> </tr> <tr> <td><a href="#link7">C Link</a></td> </tr...

Python - Sorting a list of objects

Assuming I have a list of JSON objects: obj1 = {'title': 'Super genius', 'modifiedDate': 1334014208.0, 'user_id': 123} obj2 = {'title': 'Awesome', 'modifiedDate': 1333962645.0, 'user_id': 345} obj3 = {'title': 'Gorgeous', 'modifiedDate': 1333894106.0, 'user_id': 531} obj4 = {'title': 'Cool', 'modifiedDate': 1333968061.0, 'user_id': 214} obj5 = {'title': 'Badass', 'modifiedDate': 1334016506.0, 'user_id': 678} unorder_list = [obj4, obj5, obj1, obj2, obj3] And I want to create a list contains those objects in ascending order of modifiedDate . 1. Solution #1 : using lambda to sort by 'modifiedDate' def order_list ():     order_list = unorder_list     order_list.sort(key = lambda x: x['modifiedDate'])     return order_list 2. Solution #2 : do not use lambda def order_list ():     or...