Tag Archive for sort

Sort a multi-dimensional field

function orderBy($data, $field) {
	$code = "return strnatcmp($a['$field'], $b['$field']);";
	usort($data, create_function('$a,$b', $code));
	return $data;
}

source

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

Get highest integer from an array

var largest = Math.max.apply(Math, myArray);

source

sorting a dictionary

You could use:

sorted(dictionaryObject.items(), key=lambda x: x[1] )

source

Bubblesort

public class BubbleSort { 
   public static void sortiere(int[] x) {
      boolean unsortiert=true;
      int temp;
      
      while (unsortiert){
         unsortiert = false;
         for (int i=0; i < x.length-1; i++) 
            if (x[i] > x[i+1]) {                      
               temp       = x[i];
               x[i]       = x[i+1];
               x[i+1]     = temp;
               unsortiert = true;
            }          
      } 
   }
   
   public static void main(String[] args) {
      int[] liste = {0,9,4,6,2,8,5,1,7,3};
      sortiere(liste);
      for (int i=0; i<liste.length; i++) 
         System.out.print(liste[i]+" ");    
   } 
}

source

SHELL SCRIPT PARSING LOGFILE

#!/bin/bash
 
#initialize temp files
echo "" > topimages.txt
echo "" > images.txt
echo "" > unique.txt
echo "" > all.txt
 
if [ "$1" = "" ]; then
        echo -e "normally use like this:"
        echo -e "	./getTopPages.sh $1"
        echo -e "	get all jpg's -- ./getTopPages.sh jpg"
        echo -e "	get everything in photos -- ./getTopPages.sh photos"
        echo -e "*****" 
        exit
fi 
 
#live server logfile
logfile=/var/log/httpd/...
 
grep $1 $logfile| awk '{print $7}' | sort > all.txt
echo found: $(grep '^.*$' all.txt -c) images total...
 
grep $1 $logfile| awk '{print $7}' | sort | uniq > unique.txt
echo found: $(grep '^.*$' unique.txt -c) unique images...
 
echo '<images>' > images.txt
 
        for i in $( grep '^.*$' unique.txt  ); do
 
                        cnt=$( grep $i all.txt -c )
 
                        #add leading zeros
                        if test $cnt -lt 10; then
                                 lzcnt=0000$cnt
                        elif test $cnt -lt 100; then
                                 lzcnt=000$cnt
                        elif test $cnt -lt 1000; then
                                 lzcnt=00$cnt
                        elif test $cnt -lt 10000; then
                                 lzcnt=0$cnt
                        elif test $cnt -lt 100000; then
                                 lzcnt=$cnt
                        else
                            	 lzcnt=$cnt
                        fi
 
                        echo '<image count="'$lzcnt'" src="'$i'" />' >> images.txt
 
        done
 
sort images.txt -r -k17 > topimages.xml
echo '</images>' >> topimages.xml
 
echo removing temp files...
 
rm images.txt
rm unique.txt
rm all.txt
rm topimages.txt

source

Howto list all the file extension types in an SVN log dump

svn log -v > myrepo.log

perl -ne 'print qq{$1
} if m{^s+.*(.[^.s]w+)$}' myrepo.log | sort | uniq -c | sort -r

source

Ruby class for easy version number natural order sorting

class Version
include Comparable

attr_reader :major, :feature_group, :feature, :bugfix

def initialize(version="")
v = version.split(".")
@major = v[0].to_i
@feature_group = v[1].to_i
@feature = v[2].to_i
@bugfix = v[3].to_i
end

def <=>(other)
return @major <=> other.major if ((@major <=> other.major) != 0)
return @feature_group <=> other.feature_group if ((@feature_group <=> other.feature_group) != 0)
return @feature <=> other.feature if ((@feature <=> other.feature) != 0)
return @bugfix <=> other.bugfix
end

def self.sort
self.sort!{|a,b| a <=> b}
end

def to_s
@major.to_s + "." + @feature_group.to_s + "." + @feature.to_s + "." + @bugfix.to_s
end
end

## Example Usage:
##  list = []
##  ["1.2.2.10","1.2.2.1","2.1.10.2","2.3.4.1"].each {|v| list.push(Version.new(v)) }
##  list.sort.each{|v| pp v.to_s }
##
###"1.2.2.1"
###"1.2.2.10"
###"2.1.10.2"
###"2.3.4.1"

source

List Directory Contents by Date ASC

ls -alrt

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