<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Is it possible to sort or reorder a multivalue field? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39429#M8998</link>
    <description>&lt;P&gt;For anyone following along at home, who hasn't looked in the docs first, ....&lt;/P&gt;

&lt;P&gt;There is now a  builtin &lt;A href="http://docs.splunk.com/Documentation/Splunk/7.0.3/SearchReference/MultivalueEvalFunctions#mvdedup.28X.29"&gt;mvdedup&lt;/A&gt; eval function.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Apr 2018 20:39:34 GMT</pubDate>
    <dc:creator>Lowell</dc:creator>
    <dc:date>2018-04-05T20:39:34Z</dc:date>
    <item>
      <title>Is it possible to sort or reorder a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39425#M8994</link>
      <description>&lt;P&gt;Anyone have any thoughts as to how to reorder a multi-valued field?  Ideally I'd like to be able to do a "sort" or in my specific use case, a "reverse" would be perfect.&lt;/P&gt;

&lt;P&gt;Say you have the following search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;my search | stats list(myfield) as myfields by id
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The &lt;CODE&gt;list()&lt;/CODE&gt; stats operator preserves all values of "myfield" in the events and preserves order, which is what I want.   However, I'd really like to see the values of "myfield" in time order (not reverse time order.)  I know I can stick a &lt;CODE&gt;| reverse&lt;/CODE&gt; in there, but I was trying to figure out if there was a better approach that only modifies the "myfields" field, and doesn't require screwing with event order.&lt;/P&gt;

&lt;P&gt;(In my non-trivial version of this search, I'm using a &lt;CODE&gt;transaction&lt;/CODE&gt; command as well, and it has issues when you start messing with time-order.  That's just one example of why re-ordering the events is not ideal.)&lt;/P&gt;</description>
      <pubDate>Sat, 05 Feb 2011 02:27:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39425#M8994</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-02-05T02:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to sort or reorder a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39426#M8995</link>
      <description>&lt;P&gt;Hi Lowell, I implemented the deduplication and sorting functionality in a custom command. Being your experience far greater than mine you won't have any problem to remove the deduplication logic (and maybe suggest any improvement &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Syntax:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mvdedup [[+|-]fieldname ]*
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;UL&gt;
&lt;LI&gt;with no parameter: will dedup all the multivalued fields retaining their order&lt;/LI&gt;
&lt;LI&gt;with one or more fieldnames: will dedup those fields retaining their order&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;with one or more fieldnames prepended by a +|- (no empty space there!): will dedup and sort ascending/descending&lt;/P&gt;

&lt;P&gt;| mvdedup -id&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Here's the configs:&lt;/P&gt;

&lt;P&gt;commands.conf&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[mvdedup]
type = python
streaming = true
maxinputs = 500000
run_in_preview = true
enableheader = true
retainsevents = true
generating = false
generates_timeorder = false
supports_multivalues = true
supports_getinfo = true
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;mvdedup.py&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import sys
import splunk.Intersplunk as si
import string

def uniqfy(seq,sortorder=None):
    seen = {}
    result = []
    for item in seq:
            if item in seen: continue
            seen[item] = 1
            result.append(item)
    if sortorder=='+':
        result.sort()
    elif sortorder=='-':
        result.sort(reverse=True)
    return result

(isgetinfo, sys.argv) = si.isGetInfo(sys.argv)

if isgetinfo:
    #outputInfo(streaming, generating, retevs, reqsop, preop, timeorder=False):
    si.outputInfo(True, False, True, False, None, False)
    sys.exit(0)


results = si.readResults(None, None, True)

fields={}

if len(sys.argv) &amp;amp;gt; 1:
    for a in sys.argv[1:]:
        a=str(a)
        if a[:1] in ['+','-']:
            # set sorting order
            fields[a[1:]] = a[:1]
        else:
            # no sorting!
            fields[a] = None
else:
    # dedup on all the fields in the data
    for k in results[0].keys():
        fields[k] = None

for i in range(len(results)):
    for key in results[i].keys():
        if(isinstance(results[i][key], list)):    
            if key in fields.keys():
                results[i][key] = uniqfy(results[i][key],fields[key])

si.outputResults(results)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any suggestion is more than welcome&lt;/P&gt;</description>
      <pubDate>Mon, 07 Feb 2011 03:24:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39426#M8995</guid>
      <dc:creator>Paolo_Prigione</dc:creator>
      <dc:date>2011-02-07T03:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to sort or reorder a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39427#M8996</link>
      <description>&lt;P&gt;I have the same need, and if I solve it by adding in the "reverse" command before the stats, it reduces my search performance by almost 40%.  I'm eager for another answer here that doesnt involve a custom python command.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2011 19:05:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39427#M8996</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2011-06-02T19:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to sort or reorder a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39428#M8997</link>
      <description>&lt;P&gt;Thanks for this answer.  It helped me answer my question - &lt;A href="http://answers.splunk.com/answers/115137/joining-across-field-matrix"&gt;http://answers.splunk.com/answers/115137/joining-across-field-matrix&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Dec 2013 16:51:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39428#M8997</guid>
      <dc:creator>rizzo75</dc:creator>
      <dc:date>2013-12-14T16:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to sort or reorder a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39429#M8998</link>
      <description>&lt;P&gt;For anyone following along at home, who hasn't looked in the docs first, ....&lt;/P&gt;

&lt;P&gt;There is now a  builtin &lt;A href="http://docs.splunk.com/Documentation/Splunk/7.0.3/SearchReference/MultivalueEvalFunctions#mvdedup.28X.29"&gt;mvdedup&lt;/A&gt; eval function.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 20:39:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39429#M8998</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2018-04-05T20:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to sort or reorder a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39430#M8999</link>
      <description>&lt;P&gt;Both mvdedup and mvsort were added as evaluation functions (ie you can use them in eval and where) in 6.2.   &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval mvfield=mvsort(mvfield)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;and &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval mvfield=mvdedup(mvfield)&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 18:16:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-sort-or-reorder-a-multivalue-field/m-p/39430#M8999</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2019-04-15T18:16:53Z</dc:date>
    </item>
  </channel>
</rss>

