Skip to content

Commit 560d160

Browse files
posvayyx990803
authored andcommitted
Update orderBy (vuejs#269)
* Update orderBy * Fix suggestions on orderBy api update * Add multiple keys for orderBy * Update index.md
1 parent 786f58f commit 560d160

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

src/api/index.md

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,14 +2101,14 @@ type: api
21012101
- **Limited to:** directives that expect `Array` values, e.g. `v-for`
21022102

21032103
- **Arguments:**
2104-
- `{String} sortKey`
2104+
- `{String | Array<String> | Function} ...sortKeys`
21052105
- `{String} [order] - default: 1`
21062106

21072107
- **Usage:**
21082108

2109-
Return a sorted version of the source Array. The `sortKey` is the key to use for the sorting. The optional `order` argument specifies whether the result should be in ascending (`order >= 0`) or descending (`order < 0`) order.
2109+
Return a sorted version of the source Array. You can pass any number of Strings to sort on keys. You can also pass an array containing the sorting keys or a Function if you want to use your own sorting strategy instead. The optional `order` argument specifies whether the result should be in ascending (`order >= 0`) or descending (`order < 0`) order.
21102110

2111-
For arrays of primitive values, any truthy `sortKey` will work.
2111+
For arrays of primitive values, set `sortKey` to `true`.
21122112

21132113
- **Example:**
21142114

@@ -2165,6 +2165,16 @@ type: api
21652165
})
21662166
```
21672167

2168+
Sort using two keys:
2169+
2170+
``` html
2171+
<ul>
2172+
<li v-for="user in users | orderBy 'lastName' 'firstName'">
2173+
{{ user.lastName }} {{ user.firstName }}
2174+
</li>
2175+
</ul>
2176+
```
2177+
21682178
{% raw %}
21692179
<div id="orderby-example" class="demo">
21702180
<button @click="order = order * -1">Reverse Sort Order</button>
@@ -2185,6 +2195,85 @@ type: api
21852195
</script>
21862196
{% endraw %}
21872197

2198+
Sort using a Function:
2199+
2200+
``` html
2201+
<div id="orderby-compare-example" class="demo">
2202+
<button @click="order = order * -1">Reverse Sort Order</button>
2203+
<ul>
2204+
<li v-for="user in users | orderBy ageByTen">
2205+
{{ user.name }} - {{ user.age }}
2206+
</li>
2207+
</ul>
2208+
</div>
2209+
```
2210+
2211+
``` js
2212+
new Vue({
2213+
el: '#orderby-compare-example',
2214+
data: {
2215+
order: 1,
2216+
users: [
2217+
{
2218+
name: 'Jackie',
2219+
age: 62
2220+
},
2221+
{
2222+
name: 'Chuck',
2223+
age: 76
2224+
},
2225+
{
2226+
name: 'Bruce',
2227+
age: 61
2228+
}
2229+
]
2230+
},
2231+
methods: {
2232+
ageByTen: function (a, b) {
2233+
return Math.floor(a.age / 10) - Math.floor(b.age / 10)
2234+
}
2235+
}
2236+
})
2237+
```
2238+
2239+
{% raw %}
2240+
<div id="orderby-compare-example" class="demo">
2241+
<button @click="order = order * -1">Reverse Sort Order</button>
2242+
<ul id="orderby-compare-example">
2243+
<li v-for="user in users | orderBy ageByTen order">
2244+
{{ user.name }} - {{ user.age }}
2245+
</li>
2246+
</ul>
2247+
</div>
2248+
<script>
2249+
new Vue({
2250+
el: '#orderby-compare-example',
2251+
data: {
2252+
order: 1,
2253+
users: [
2254+
{
2255+
name: 'Jackie',
2256+
age: 62
2257+
},
2258+
{
2259+
name: 'Chuck',
2260+
age: 76
2261+
},
2262+
{
2263+
name: 'Bruce',
2264+
age: 61
2265+
}
2266+
]
2267+
},
2268+
methods: {
2269+
ageByTen: function (a, b) {
2270+
return Math.floor(a.age / 10) - Math.floor(b.age / 10)
2271+
}
2272+
}
2273+
})
2274+
</script>
2275+
{% endraw %}
2276+
21882277
## Array Extension Methods
21892278

21902279
Vue.js extends `Array.prototype` with two additional methods that makes it easier to perform some common Array operations while ensuring reactive updates are properly triggered.

0 commit comments

Comments
 (0)