On 07/21/2014 05:06 PM, Chris Dieringer wrote:
Hi all:
If I have the following strings:
1. Week 1 2. Week 10 3. Week 2
They will be sorted in that order using a traditional
orderBy:thatPropertyName. This is probably less of an angular
question and more of a js question, but thought I'd ask regardless.
If I want the above to be sorted properly, is there a quick trick?
One way that you can do this is to pass a function to sort()
to do the sorting that way you want to:
<script>
var strings = ["Week 1", "Week 10", "Week 2"];
document.write(strings.sort(sort_num));
function sort_num(left, right){
return parseInt(left.split(' ')[1]) > parseInt(right.split(' ')[1]);
}
</script>
This puts out:
Week 1, Week 2, Week 10
If the strings are less predictable, you
could use regular expressions to remove
non-numeric characters before the parseInt().
Tobiah
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.