Last active
November 12, 2016 07:49
-
-
Save deepakshrma/c14cb39b113d2802919cd73c80f8ad0d to your computer and use it in GitHub Desktop.
PrioritySorting
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
//Problem to sort and get port with minimum bandwith, based on switch priority(Select same switch first) | |
var ports = [{ | |
id: 1, | |
switch: 'a', | |
availableBandWidth: 500 | |
},{ | |
id: 2, | |
switch: 'b', | |
availableBandWidth: 10 | |
},{ | |
id: 3, | |
switch: 'a', | |
availableBandWidth: 50 | |
},{ | |
id: 4, | |
switch: 'b', | |
availableBandWidth: 100 | |
},{ | |
id: 5, | |
switch: 'a', | |
availableBandWidth: 100 | |
},{ | |
id: 6, | |
switch: 'b', | |
availableBandWidth: 5 | |
} | |
]; | |
var reqSwitch = 'a'; | |
var selected; | |
function getMinPort(p1, p2){ | |
if (p1.switch == reqSwitch && p2.switch == reqSwitch) { | |
return p1.availableBandWidth - p2.availableBandWidth; | |
} | |
else if (p1.switch == reqSwitch && p2.switch != reqSwitch) { | |
return -1; | |
} | |
else if (p2.switch == reqSwitch && p1.s != reqSwitch) { | |
return 1 | |
} | |
else { | |
return p1.availableBandWidth - p2.availableBandWidth; | |
} | |
} | |
console.log("Best selected:", ports.sort(getMinPort)[0]); | |
reqSwitch = 'b'; | |
console.log("Best selected:", ports.sort(getMinPort)[0]); | |
reqSwitch = 'c'; | |
console.log("Best selected:", ports.sort(getMinPort)[0]); | |
//Output | |
//Best selected: { id: 3, switch: 'a', availableBandWidth: 50 } | |
//Best selected: { id: 6, switch: 'b', availableBandWidth: 5 } | |
//Best selected: { id: 6, switch: 'b', availableBandWidth: 5 } | |
var utils = { | |
sortByPriority:sortByPriority | |
}; | |
function sortByPriority(arr, comparator, priorityComparator){ | |
if(!arr || !arr.length){ | |
return arr; | |
} | |
function combiner(p1, p2){ | |
if(priorityComparator(p1) && !priorityComparator(p2)){ | |
return -1; | |
} | |
else if(!priorityComparator(p1) && priorityComparator(p2)){ | |
return 1; | |
} | |
else { | |
return comparator(p1, p2); | |
} | |
} | |
return arr.sort(combiner); | |
} | |
function portWithMinBandWidth(p1, p2){ | |
return p1.availableBandWidth - p2.availableBandWidth | |
} | |
function switchPriority(p1){ | |
return reqSwitch == p1.switch | |
} | |
reqSwitch ='a'; | |
var sortedData = utils.sortByPriority(ports, portWithMinBandWidth,switchPriority); | |
console.log("Best selected:", sortedData[0]); | |
reqSwitch ='b'; | |
sortedData = utils.sortByPriority(ports, portWithMinBandWidth,switchPriority); | |
console.log("Best selected:", sortedData[0]); | |
reqSwitch ='c'; | |
sortedData = utils.sortByPriority(ports, portWithMinBandWidth,switchPriority); | |
console.log("Best selected:", sortedData[0]); | |
//Output | |
//Best selected: { id: 3, switch: 'a', availableBandWidth: 50 } | |
//Best selected: { id: 6, switch: 'b', availableBandWidth: 5 } | |
//Best selected: { id: 6, switch: 'b', availableBandWidth: 5 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment