Tag Archive for sorting

Insert a value into a numerically-ordered array in the correct order

def insert_value_in_order(array_of_values, new_value, update_array = nil)
	value_index = array_of_values.index(new_value)
	index = 0
	if !value_index && array_of_values.last.to_i > new_value
		i = 0
		while array_of_values[i].to_i < new_value do
			i += 1
		end
		index = i
	else
		index = value_index ? value_index : -1
	end
	return update_array == true || update_array == 1 ? array_of_values.insert(index, new_value) : index
end

source

Sort Objects by Date Property

Array.prototype.sortByDate = function(sortOrder) {
	var objArray = this;    // [Obj, Obj, Obj] where each Obj 
                                // has prop: "sortDate" with val: "YYYY-MM-DD"
	
	var sBD = function(a, b) {
		var dateStringA = a.sortDate.split("-");
		var dateStringB = b.sortDate.split("-");
		var dateA = new Date(dateStringA[0], dateStringA[1], dateStringA[2]);
		var dateB = new Date(dateStringB[0], dateStringB[1], dateStringB[2]);
		return dateA - dateB;
	};

	if (sortOrder == "desc") {
		objArray.sort(sBD).reverse();
	} else if (sortOrder == "asc") {
		objArray.sort(sBD);
	} else if (!sortOrder) {
		objArray.sort(sBD);
	}
}

source

/*
objSort v 1.1
copyright 2006 Thomas Frank

This program is free software under the terms of the
GNU General Public License version 2 as published by the Free
Software Foundation. It is distributed without any warranty.
*/

tfObjSort={
init:function(){
Array.prototype.objSort=function(){
tfObjSort.setThings(this);
var a=arguments;
var x=tfObjSort;
x.a=[];x.d=[];
for(var i=0;i<a.length;i++){
if(typeof a[i]=="string"){x.a.push(a[i]);x.d.push(1)};
if(a[i]===-1){x.d[x.d.length-1]=-1}
}
return this.sort(tfObjSort.sorter);
};
Array.prototype.strSort=function(){
tfObjSort.setThings(this);
return this.sort(tfObjSort.charSorter)
}
},
sorter:function(x,y){
var a=tfObjSort.a
var d=tfObjSort.d
var r=0
for(var i=0;i<a.length;i++){
if(typeof x+typeof y!="objectobject"){return typeof x=="object"?-1:1};
var m=x[a[i]]; var n=y[a[i]];
var t=typeof m+typeof n;
if(t=="booleanboolean"){m*=-1;n*=-1}
else if(t.split("string").join("").split("number").join("")!=""){continue};
r=m-n;
if(isNaN(r)){r=tfObjSort.charSorter(m,n)};
if(r!=0){return r*d[i]}
}
return r
},
charSorter:function(x,y){
if(tfObjSort.ignoreCase){x=x.toLowerCase();y=y.toLowerCase()};
var s=tfObjSort.chars;
if(!s){return x>y?1:x<y?-1:0};
x=x.split("");y=y.split("");l=x.length>y.length?y.length:x.length;
var p=0;
for(var i=0;i<l;i++){
p=s.indexOf(x[i])-s.indexOf(y[i]);
if(p!=0){break};
};
if(p==0){p=x.length-y.length};
return p
},
setThings:function(x){
this.ignoreCase=x.sortIgnoreCase;
var s=x.sortCharOrder;
if(!s){this.chars=false;return true};
if(!s.sort){s=s.split(",")};
var a="";
for(var i=1;i<1024;i++){a+=String.fromCharCode(i)};
for(var i=0;i<s.length;i++){
z=s[i].split("");
var m=z[0]; var n=z[1]; var o="";
if(z[2]=="_"){o=n+m} else {o=m+n};
a=a.split(m).join("").split(n).join(o);
};
this.chars=a
}
};
tfObjSort.init();

source

Sorting by multiple columns

function multi_tablesort($header = array()) {
$sorts = array('asc', 'desc');
$sorter = '';
$tablesort = array();

foreach($header AS $field) {
if (in_array($field['sort'],$sorts)) {
$tablesort[] = db_escape_string($field['field']) . ' ' . drupal_strtoupper(db_escape_string($field['sort']));
}

}

if (count($tablesort) > 0) {
$sorter = 'ORDER BY ' . implode(', ', $tablesort);
}

return $sorter;
}

source

Sorting MySQL data in a GRID

<?php
//basic mysql connection, you'd want to use a DB class or something
$connect = @mysql_connect("localhost","username","password") or die("Could not connect to server.");
$db = @mysql_select_db("database") or die(mysql_error());
$query = "SELECT * FROM table";
$result = @mysql_query($query) or die("Could not execute query");
$check = mysql_num_rows($result);
if ($check == 0) {
echo "Table is empty.";
} else {
?>
<table>
<?php
$i = 1;
while ($row = mysql_fetch_array($result)) {
if ($i == 1) {
?>
<tr>
<?php
}
?>
<td><?php echo $row['column']; ?></td>
<?php
if ($i == 7) { /* here we have 7 columns per row, if you want 10 columns just change this value to 10 instead of 7 */
$i = 0;
?>
</tr>
<?php
}
$i++;
}
?>
</tr>
</table>
<?php
}
?>

source

Quicksort

function qsort(a){
if (a.length <= 1){return a;}
var p = a[0];
var gr = [];
var ls = [];
for(i=1;i<a.length;i++){
if (a[i] < p){ls.push(a[i]);}
else{gr.push(a[i]);}
}
ls = qsort(ls);
gr = qsort(gr);
ls.push(p)
return ls.concat(gr);
}

source

Random Sorting

SELECT *
FROM Northwind..Orders
ORDER BY NEWID()

source

Sort by random character

order by case when left([text], 1) >= CHAR(65 + 26 * rand()) then 0 else 1 end

source