Last active
February 24, 2017 17:43
-
-
Save AshleyGrant/27a6e61996cb884d97fbeed0acb310bf to your computer and use it in GitHub Desktop.
Grouped and Sorted Array Using a Value Converter
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
<template> | |
<require from="./grouped-sort"></require> | |
<table> | |
<template repeat.for="[group, items] of myArray | groupedSort"> | |
<tr> | |
<td> | |
${group} | |
</td> | |
</tr> | |
<tr repeat.for="item of items"> | |
<td> | |
${item.name} | |
</td> | |
</tr> | |
</template> | |
</table> | |
</template> |
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
export class App { | |
myArray = [ | |
{ | |
name: 'R2-D2', | |
group: 'robots' | |
}, | |
{ | |
name: 'terminator', | |
group: 'robots' | |
}, | |
{ | |
name: 'wall-e', | |
group: 'robots' | |
}, | |
{ | |
name: 'robocop', | |
group: 'robots' | |
}, | |
{ | |
name: 'C-3PO', | |
group: 'robots' | |
}, | |
{ | |
name: 'dog', | |
group: 'animals' | |
}, | |
{ | |
name: 'shark', | |
group: 'animals' | |
}, | |
{ | |
name: 'cat', | |
group: 'animals' | |
}, | |
{ | |
name: 'monkey', | |
group: 'animals' | |
} | |
] | |
} |
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
export class GroupedSortValueConverter { | |
toView(value) { | |
let sortedArray = value.splice(0).sort((a, b) => { | |
const left = a.name, | |
right = b.name; | |
return caseInsensitiveStringComparer(left, right); | |
}); | |
const groups = new Map(); | |
for (let item of sortedArray) { | |
let group = groups.get(item.group); | |
if (group === undefined) { | |
groups.set(item.group, [item]); | |
} else { | |
group.push(item); | |
} | |
} | |
let sortedGroups = new Map(Array.from(groups.entries()).sort((a, b) => { | |
const left = a[0]; | |
const right = b[0]; | |
return caseInsensitiveStringComparer(left, right); | |
})); | |
return sortedGroups; | |
} | |
} | |
function caseInsensitiveStringComparer(left, right) { | |
const lowerLeft = left.toLowerCase(), | |
lowerRight = right.toLowerCase(); | |
if (lowerLeft < lowerRight) { //sort string ascending | |
return -1; | |
} else if (lowerLeft > lowerRight) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} |
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> | |
<title>Aurelia</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body aurelia-app> | |
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script> | |
<script> | |
require(['aurelia-bootstrapper']); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment