Simple JSON Object and Array sort.
- Sort JSON with comments.
- Sort Ascending
- Sort Descending
- Sort Randomize
- Sort By Value
- Sort By Key
- Sort By Value Length
- Sort By Key Length
- Sort By Value Type
- Sort By Custom Comparison.
- Sort Object
- Sort List items
- Sort Collections (Array of object) by its key
- Override object sort key order
- Sort by case sensitive and insensitive
- Set Nested Sort level
- Supports sorting Partially selected json text
- Supports preserving BigInt
- Supports preserving unicode
- Supports Quick Fix and Fix All code action
- sort-json.settings.sortMode: Set the sort mode for JSON. Options: ["Both", "Lists Only", "Objects Only"]. Default: "Both".
- sort-json.settings.sortLevel: Set the depth of sorting. Default: -1. -1 sorts all nested object.
- sort-json.settings.objectSortType: Set the sorting type for JSON objects. Options: ["Key", "Key Length", "Value", "Value Length", "Value Type"]. Default: "Key".
- sort-json.settings.listSortType: Set the sorting type for JSON lists. Options: ["Value", "Value Length", "Value Type"]. Default: "Value".
- sort-json.settings.sortValueTypeOrder: Set the order of value types to sort. Options: ["Boolean", "Null", "Number", "String", "Array", "Collection", "PlainObject"].
- sort-json.settings.isCaseSensitive: Set to compare with case sensitivity in sorting. Default: False.
- sort-json.settings.promptCollectionKeys: Prompt to select keys to sort a collection. Default: True.
- sort-json.settings.preserveUnicodeString: Set to preserve Unicode strings in sorting. Default: False.
- sort-json.settings.orderOverrideKeys: Keys to override the sort order. Default: []. Provide keys with a spread ('...') to place remaining object keys in order.
- sort-json.settings.excludePaths: Set exclude paths for deep sort. Default: []. Provide paths as strings.
- sort-json.settings.customSortComparisons: Provide custom sort comparison codes. Default: []. Provide an array of objects with "comparison" and "description" fields.
- sort-json.settings.defaultCustomSort: Provide default custom sort comparison code. Default: "". Provide a string for default custom comparison.
- sort-json.settings.contextMenu: Show or hide Sort JSON context menus. Default:
{ "sortJsonSubMenu": true, "ascendingSort": false, "descendingSort": false, "randomizeSort": false, "customSort": false, "setSortLevel": false, "setObjectSortType": false, "setListSortType": false, "isCaseSensitive": false, "promptCollectionKeys": false } - sort-json.settings.showInfoMsg: Show or hide information messages. Options: [True, False]. Default: True.
- sort-json.settings.ignoreFiles: Provide a list of file names to ignore for sorting. Default: []. Provide an array of strings.
- sort-json.settings.forceSort: Forcefully sort and write JSON even if it is already sorted. Options: [True, False]. Default: False.
- sort-json.settings.jsonFormatIndent: Set JSON formatting indent spaces. Options: [number, null]. Default: null. Provide a number or use editor tab size.
- sort-json.settings.preserveBingInt: If true it preserves the BigInt in JSON. Be cautions, this may modify the big numbers. This wont work with JSON with comments.
- sort-json.settings.convertBingIntToString: If true it converts all the BigInt in JSON to string. This works only if sort-json.settings.jsonFormatIndent set to
true.
- Right click on a file and select
Do Custom Sortcommand fromSort JSON. - A quick pick items shows up where we can provide our own custom logic to sort the data.
- We can also save our custom comparisons in settings using
sort-json.settings.customComparisonsvscode settings which will shows up in the quick pick items.
Sort Array
-
predefined variables
item1,key1,val1,value1,xis equal toa.item2,key2,val2,value2,yis equal tob._,lodash,dash- Lodash is exposed.
-
Checks
isArray,isListwill betruein array sort orderisObjectwill befalsein array sort orderisAllNumber- returnstrueif all the items in a list are numbersisAllString- returnstrueif all the items in a list are stringisAllList- returnstrueif all the items in a list are listisAllObject,isCollection- returnstrueif all the items in a list are objects
-
Examples:
-
Sort Ascending:
- Input:
[9, 2, 6, 5, 4, 1, 3, 0, 7] - Comparison code:
a - boritem1 - item2orx - y - Output:
[0, 1, 2, 3, 4, 5, 6, 7, 9]
- Input:
-
Sort by Item Length:
- Input:
["Hi", "this", "is", "a", "custom", "comparison", "sort"] - Comparison code:
item1.length - item2.length - Output:
["a", "Hi", "is", "this", "sort", "custom", "comparison"]
- Input:
-
Sort by Alphabetical Case-Insensitive Ascending:
- Input:
["Hi", "this", "is", "a", "custom", "comparison", "sort"] - Comparison code:
_.toLower(a) == _.toLower(b) ? 0 : _.toLower(a) > _.toLower(b) ? 1 : -1 - Output:
["a", "comparison", "custom", "Hi", "is", "sort", "this"]
- Input:
-
Sort Collections by ID:
- Input:
[{"id": 2, "name": "bar"}, {"id": 1, "name": "foo"}] - Comparison code:
a.id - b.id - Output:
[{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}]
- Input:
-
Sort Collections by Name:
- Input:
[{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}] - Comparison code:
a.name == b.name ? 0 : a.name > b.name ? 1 : -1 - Output:
[{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}]
- Input:
-
Sort Object
-
predefined variables
key1- Object first keykey2- Object second keyval1,value1- Object first valueval2,value2- Object second valueitem1- { key:key1, val:val1}item2- { key:key2, val:val2}a,xis equal toitem1b,yis equal toitem2_,lodash,dash- Lodash is exposed.
-
Checks
isArray,isListwill befalsein object sort orderisObjectwill betruein object sort orderisAllNumber- returnstrueif all the values in a object are numbersisAllString- returnstrueif all the values in a object are stringisAllList- returnstrueif all the values in a object are listisAllObject,isCollection- returnstrueif all the values in a list are objects
-
Examples:
-
Sort by Key Length:
- Input:
{"name": "first item", "id": 1, "label": "foo"} - Comparison code:
key1.length - key2.lengthoritem1.key.length - item2.key.length - Output:
{"id": 1, "name": "first item", "label": "foo"}
- Input:
-
Sort by Value Length:
- Input:
{"name": "foo", "id": 1, "label": "first item"} - Comparison code:
isAllString ? val1.length - val2.length : true - Output:
{"id": 1, "label": "foo", "name": "first item"}
- Input:
-
There's a vscode setting for formatters (settings.json):
"editor.codeActionsOnSave": {
"source.fixAll": "explicit" // set to "explicit" to sort json files on save
}But you can also selectively enable/disable this formatter with (settings.json):
Or use a hotkey, if you prefer (keybindings.json):
{
"key": "cmd+shift+a",
"command": "editor.action.codeAction",
"args": {
"kind": "source.fixAll.sort-json"
}
}Enjoy!
