# Kv alphabetical sorting

**URL:** <https://discuss.elastic.co/t/kv-alphabetical-sorting/136876>\
**Category:** Logstash\
**Created:** [June 21, 2018, 1:28pm UTC](https://discuss.elastic.co/t/kv-alphabetical-sorting/136876 "2018-06-21T13:28:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonviously](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonviously/32/44730_2.png) [@jonviously](https://discuss.elastic.co/u/jonviously)\
**Post date:** [June 21, 2018, 1:28pm UTC](https://discuss.elastic.co/t/kv-alphabetical-sorting/136876/1 "2018-06-21T13:28:14Z")

</div>

Hi !

I just discovered that the kv filter plugin applies an alphabetical sorting on keys in output.

For example, i got a string like "a=1&c=2&b=3"

after the kv plugin i got this :  
{  
"a" : "1"  
"b" : "3"  
"c" : "2"  
}

but i wanna keep the keys order like :

{  
"a" : "1"  
"c" : "3"  
"b" : "2"  
}

There is a solution to that except create my own filter ?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [June 21, 2018, 1:32pm UTC](https://discuss.elastic.co/t/kv-alphabetical-sorting/136876/2 "2018-06-21T13:32:39Z")

</div>

The fields are stored in a Ruby hash or a Java map (I think; I'd expect it to depend on the Logstash implementation). The order of the keys in such data structures is typically undefined so even if it looks like they're alphabetical now you shouldn't rely on it. It could also be the case that it's the JSON serializer that's sorting the keys, but since it obtains the keys from the internal hash/map it's not possible to maintain the original order.

Why does this matter?

---

<div class="post-metadata">

**Author:** ![jonviously](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonviously/32/44730_2.png) [@jonviously](https://discuss.elastic.co/u/jonviously)\
**Post date:** [June 21, 2018, 2:27pm UTC](https://discuss.elastic.co/t/kv-alphabetical-sorting/136876/3 "2018-06-21T14:27:01Z")

</div>

thanks for this explanation 🙂

it's important because i need to exclude some specifics params from the string, so i delete some keys in the array before implode on '&' to rebuild a "similar" string

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 19, 2018, 2:35pm UTC](https://discuss.elastic.co/t/kv-alphabetical-sorting/136876/4 "2018-07-19T14:35:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
