Created
November 25, 2014 20:46
-
-
Save ronsims2/ef6bf8150ad7143960e1 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/dirixi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="[add your bin description]" /> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/** | |
*@Name orderItems | |
@Param items array - a list of object to sort | |
@param field string - name of field to order by, should be , value must be numeric | |
@Param rank string name of field that holds new oeder ranking, value is a numeric. | |
@param clean bool - optional, tells function to remove added meta data | |
*/ | |
function orderItems(items, field, rank, clean){ | |
//Holds sorted items | |
var sorted = []; | |
//Tracks if sorted needs to be expanded because multiple items exist | |
var hasNested = 0; | |
//Loop through items to compare them against each other | |
for(var i = 0; i < items.length; i++){ | |
var item = items[i]; //current item being compared | |
item[rank] = 0; | |
//Loop to compare items | |
for(var j = 0; j < items.length; j++){ | |
var compItem = items[j]; | |
//Don't compare against self | |
if (i !== j) { | |
if (item[field] > compItem[field]) { | |
//Increase rank if item is greater | |
item[rank]++; | |
} | |
} | |
} | |
//Expand sorted list to size of items | |
sorted.push(null); | |
} | |
//Loop through items and place in order of rank. If a draw, get next available spot | |
for(var l = 0; l < items.length; l++){ | |
var ranking = items[l][rank]; | |
//clean up meta data | |
if (clean) { | |
delete items[l][rank]; | |
} | |
if (sorted[ranking] === null) { | |
sorted[ranking] = items[l]; | |
} | |
else{ | |
//Add to rank array | |
if (sorted[ranking] instanceof Array) { | |
sorted[ranking].push(items[l]); | |
} | |
//Create rank array if | |
else { | |
var rankContainer = [sorted[ranking], items[l]]; | |
sorted[ranking] = rankContainer; | |
hasNested++; | |
} | |
} | |
} | |
//Expand sorted if necessary | |
if (hasNested) { | |
var expanded = []; | |
for (var m = 0; m < sorted.length; m++){ | |
var itm = sorted[m]; | |
if (itm instanceof Array) { | |
for(var n = 0; n < itm.length; n++){ | |
expanded.push(itm[n]); | |
} | |
} | |
else { | |
if (itm) { | |
expanded.push(itm); | |
} | |
} | |
} | |
sorted = expanded; | |
} | |
return sorted; | |
} | |
var items = [ | |
{value:9, matches: 0, rank: 0}, | |
{value:2, matches: 0, rank: 0}, | |
{value:5, matches: 0, rank: 0}, | |
{value:6, matches: 0, rank: 0}, | |
{value:3, matches: 0, rank: 0}, | |
{value:1, matches: 0, rank: 0}, | |
{value:4, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:8, matches: 0, rank: 0}, | |
{value:5, matches: 0, rank: 0}, | |
{value:6, matches: 0, rank: 0}, | |
{value:3, matches: 0, rank: 0}, | |
{value:1, matches: 0, rank: 0} | |
]; | |
sorted = orderItems(items, "value", "atches", true); | |
console.log(sorted); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/** | |
*@Name orderItems | |
@Param items array - a list of object to sort | |
@param field string - name of field to order by, should be , value must be numeric | |
@Param rank string name of field that holds new oeder ranking, value is a numeric. | |
@param clean bool - optional, tells function to remove added meta data | |
*/ | |
function orderItems(items, field, rank, clean){ | |
//Holds sorted items | |
var sorted = []; | |
//Tracks if sorted needs to be expanded because multiple items exist | |
var hasNested = 0; | |
//Loop through items to compare them against each other | |
for(var i = 0; i < items.length; i++){ | |
var item = items[i]; //current item being compared | |
item[rank] = 0; | |
//Loop to compare items | |
for(var j = 0; j < items.length; j++){ | |
var compItem = items[j]; | |
//Don't compare against self | |
if (i !== j) { | |
if (item[field] > compItem[field]) { | |
//Increase rank if item is greater | |
item[rank]++; | |
} | |
} | |
} | |
//Expand sorted list to size of items | |
sorted.push(null); | |
} | |
//Loop through items and place in order of rank. If a draw, get next available spot | |
for(var l = 0; l < items.length; l++){ | |
var ranking = items[l][rank]; | |
//clean up meta data | |
if (clean) { | |
delete items[l][rank]; | |
} | |
if (sorted[ranking] === null) { | |
sorted[ranking] = items[l]; | |
} | |
else{ | |
//Add to rank array | |
if (sorted[ranking] instanceof Array) { | |
sorted[ranking].push(items[l]); | |
} | |
//Create rank array if | |
else { | |
var rankContainer = [sorted[ranking], items[l]]; | |
sorted[ranking] = rankContainer; | |
hasNested++; | |
} | |
} | |
} | |
//Expand sorted if necessary | |
if (hasNested) { | |
var expanded = []; | |
for (var m = 0; m < sorted.length; m++){ | |
var itm = sorted[m]; | |
if (itm instanceof Array) { | |
for(var n = 0; n < itm.length; n++){ | |
expanded.push(itm[n]); | |
} | |
} | |
else { | |
if (itm) { | |
expanded.push(itm); | |
} | |
} | |
} | |
sorted = expanded; | |
} | |
return sorted; | |
} | |
var items = [ | |
{value:9, matches: 0, rank: 0}, | |
{value:2, matches: 0, rank: 0}, | |
{value:5, matches: 0, rank: 0}, | |
{value:6, matches: 0, rank: 0}, | |
{value:3, matches: 0, rank: 0}, | |
{value:1, matches: 0, rank: 0}, | |
{value:4, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:8, matches: 0, rank: 0}, | |
{value:5, matches: 0, rank: 0}, | |
{value:6, matches: 0, rank: 0}, | |
{value:3, matches: 0, rank: 0}, | |
{value:1, matches: 0, rank: 0} | |
]; | |
sorted = orderItems(items, "value", "atches", true); | |
console.log(sorted);</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*@Name orderItems | |
@Param items array - a list of object to sort | |
@param field string - name of field to order by, should be , value must be numeric | |
@Param rank string name of field that holds new oeder ranking, value is a numeric. | |
@param clean bool - optional, tells function to remove added meta data | |
*/ | |
function orderItems(items, field, rank, clean){ | |
//Holds sorted items | |
var sorted = []; | |
//Tracks if sorted needs to be expanded because multiple items exist | |
var hasNested = 0; | |
//Loop through items to compare them against each other | |
for(var i = 0; i < items.length; i++){ | |
var item = items[i]; //current item being compared | |
item[rank] = 0; | |
//Loop to compare items | |
for(var j = 0; j < items.length; j++){ | |
var compItem = items[j]; | |
//Don't compare against self | |
if (i !== j) { | |
if (item[field] > compItem[field]) { | |
//Increase rank if item is greater | |
item[rank]++; | |
} | |
} | |
} | |
//Expand sorted list to size of items | |
sorted.push(null); | |
} | |
//Loop through items and place in order of rank. If a draw, get next available spot | |
for(var l = 0; l < items.length; l++){ | |
var ranking = items[l][rank]; | |
//clean up meta data | |
if (clean) { | |
delete items[l][rank]; | |
} | |
if (sorted[ranking] === null) { | |
sorted[ranking] = items[l]; | |
} | |
else{ | |
//Add to rank array | |
if (sorted[ranking] instanceof Array) { | |
sorted[ranking].push(items[l]); | |
} | |
//Create rank array if | |
else { | |
var rankContainer = [sorted[ranking], items[l]]; | |
sorted[ranking] = rankContainer; | |
hasNested++; | |
} | |
} | |
} | |
//Expand sorted if necessary | |
if (hasNested) { | |
var expanded = []; | |
for (var m = 0; m < sorted.length; m++){ | |
var itm = sorted[m]; | |
if (itm instanceof Array) { | |
for(var n = 0; n < itm.length; n++){ | |
expanded.push(itm[n]); | |
} | |
} | |
else { | |
if (itm) { | |
expanded.push(itm); | |
} | |
} | |
} | |
sorted = expanded; | |
} | |
return sorted; | |
} | |
var items = [ | |
{value:9, matches: 0, rank: 0}, | |
{value:2, matches: 0, rank: 0}, | |
{value:5, matches: 0, rank: 0}, | |
{value:6, matches: 0, rank: 0}, | |
{value:3, matches: 0, rank: 0}, | |
{value:1, matches: 0, rank: 0}, | |
{value:4, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:7, matches: 0, rank: 0}, | |
{value:8, matches: 0, rank: 0}, | |
{value:5, matches: 0, rank: 0}, | |
{value:6, matches: 0, rank: 0}, | |
{value:3, matches: 0, rank: 0}, | |
{value:1, matches: 0, rank: 0} | |
]; | |
sorted = orderItems(items, "value", "atches", true); | |
console.log(sorted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment