logo
Tags down

shadow

Sort in ascending and descending column by defined blocks of rows


By : J VV
Date : July 29 2020, 05:00 PM
I wish this helpful for you With GNU awk for sorted_in:
code :
$ cat tst.awk
{ block[$2] = block[$2] $0 ORS }
!(NR % 5) { prt() }
END { prt() }

function prt(   i,j) {
    PROCINFO["sorted_in"] = "@ind_num_" ( (++inst) % 2 ? "asc" : "desc" )
    for (i in block) {
        printf "%s", block[i]
    }
    delete block
}
$ awk -f tst.awk file
P 45683.00  39785.00 1 12
P 45685.00  39785.00 1 12
P 45687.00  39785.00 1 12
P 45689.00  39785.00 1 12
P 45691.00  39785.00 1 12
P 45691.00  39795.00 1 12
P 45689.00  39795.00 1 12
P 45687.00  39795.00 1 12
P 45685.00  39795.00 1 12
P 45683.00  39795.00 1 12
P 45683.00  39805.00 1 12
P 45685.00  39805.00 1 12
P 45687.00  39805.00 1 12
P 45689.00  39805.00 1 12
P 45691.00  39805.00 1 12
P 45691.00  39815.00 1 12
P 45689.00  39815.00 1 12
P 45687.00  39815.00 1 12
P 45685.00  39815.00 1 12
P 45683.00  39815.00 1 12
{ block[$2] = block[$2] $0 ORS }
!(NR % 5) { prt() }
$3 != prev { prt(); prev=$3 }
{ block[$2] = block[$2] $0 ORS }


Share : facebook icon twitter icon

How to sort in ascending or descending by a column all my rows of a CSV file in C#. Without using Function Split


By : Will
Date : March 29 2020, 07:55 AM
I hope this helps you . You have to extract value anyway. If you do not want use Split, it's not very useful since in this case can consume memory when it's not necessary, you can g through row and count amount of comma to get value you need. For this purpose I use extenssion method IndexOfNth in my example and then just find index and length which allows to extract value from row by using Substring.
In example data is sorted by column Name, index of column is 1
code :
using System;
using System.Collections.Generic;

public static class StringExtenssion 
{
    public static int IndexOfNth(this string input, string value, int startIndex, int nth)
    {
        if (nth < 1)
            throw new NotSupportedException("Param 'nth' must be greater than 0!");
        if (nth == 1)
            return input.IndexOf(value, startIndex);
        var idx = input.IndexOf(value, startIndex);
        if (idx == -1)
            return -1;
        return input.IndexOfNth(value, idx + 1, --nth);
    }
}

public class Program
{
    public static string GetColumnDataValue(string row, int column)
    {
        int startIndex = 0;
        int length = row.Length;
        if (column != 0)
            startIndex = row.IndexOfNth(",", 0, column) + 1;

        var indexOfNextComma = row.IndexOf(",", startIndex);
        if (indexOfNextComma == -1)
            length = row.Length - startIndex - 1;
        else
            length = indexOfNextComma - startIndex;

        return row.Substring(startIndex, length);
    }

    public static void Main()
    {       
        List<string> list = new List<string> {
            "2,Sam,500.00",
            "6,Mike,400.00",
            "8,Robert,156.00",
            "3,Steve,100.85",
            "9,Anderson,234.90",
        };

        var sortBy = 1;

        list.Sort((a,b) => GetColumnDataValue(a, sortBy).CompareTo(GetColumnDataValue(b, sortBy)));
        foreach(var row in list)
        {
            Console.WriteLine(row);
        }
    }
}

How to sort a numpy 2D array rows descending or ascending using a specific column indices


By : Jordi Puigsegur Figu
Date : March 29 2020, 07:55 AM
hop of those help? Here is how you can do it using the numpy_indexed package:
code :
import numpy_indexed as npi
print(a[npi.argsort((a[:,1], -a[:,0]))])

How to sort an hash Reference in perl by descending/ascending values and ascending/descending keys?


By : Valarie Mance
Date : March 29 2020, 07:55 AM
help you fix your problem Sort is using an expression to work out what order to sort things in, so it's not limited to just comparing one thing. In your case you want something like this...
code :
my @sorted_values = map $hey{$_}, sort {$hey{$b} cmp $hey{$a} or $a <=> $b} keys %hey;

Column sort reset of ascending/descending on 3rd click


By : GVS31
Date : March 29 2020, 07:55 AM
wish of those help Today I was trying to achieve same thing. I've skimmed through SlickGrid code but didn't find any 'Reset' function. So, I've tweaked the slick.grid.js v2.2 a little bit.
Basically you just need to add a new array - 'latestSortColumns' which will store sort columns state (deep copy of internal SlickGrid sortColumns array).

How to use pandas groupby to sort some rows descending and some rows ascending


By : NICK7004
Date : March 29 2020, 07:55 AM
it helps some times What is happening is that groupby.apply doesn't have an axis parameter as the error suggests, you could do this instead:
code :
df.groupby(
    ['base_currency', 'type'], group_keys=False
).apply(
    lambda g: g.sort_values('price', ascending=g.name[1] == 'bid')     
    # check the type for each group and sort accordingly
)

#   base_currency  base_volume   price type
#3            EUR        0.364   61.41  ask
#2            EUR        0.266   61.37  ask
#1            EUR        7.642   61.36  ask
#0            EUR        0.031   61.17  ask
#7            EUR        0.133   60.45  bid
#6            EUR        2.061   60.46  bid
#5            EUR       23.090   60.47  bid
#4            EUR        0.101   61.10  bid
#11           USD        3.364  161.41  ask
#10           USD        3.266  161.37  ask
#9            USD       10.642  161.36  ask
#8            USD        3.031  161.17  ask
#15           USD        3.133  160.45  bid
#14           USD        5.061  160.46  bid
#13           USD       26.090  160.47  bid
#12           USD        3.101  161.10  bid
shadow
Privacy Policy - Terms - Contact Us © voile276.org