AMONG THE SORTING TECHNIQUES, INSERTION SORT IS SELECTED FOR THIS EXERCISE
            because it gives the sort within the sort of previously available order.
            Exceptions:
            The date column is sorted on serial number within date and 
            the DIFF IN DAYS COLUMN is sorted on serial number within the days of difference.
            All oteres as per previously sorted order.
            For example if you sort on SCRIP  after sorting on date of investment,
            the sort will be on date of investment withing the SCRIP
            For the current sample data these may seem to be irrelevant.
        
JAVASCRIPT inser and inssort functions
function insert(L,i,j)
{
    tmp=L[j];
    for (var k=j;k>i;k--) // in range(j,i,-1):
        L[k]=L[k-1];
    L[i]=tmp;
}

function  inssort(list,comp)
{
    if (comp==undefined) comp=compin;
    cmp=comp;
    for (var i=0;i<list.length;i++) // in range(len(list)):
        for (var j=i+1;j<list.length;j++) //j in range(i+1,len(list)):
        if (comp(list[j],list[i]))
            insert(list,i,j)
}

//************* END OF JAVASCRIPT FUNCTION ***********/

                PYTHON PROGRAM IN FULL

dic1={'inserial':1,'dateoff':20200805,'scrip':'Bank of Baroda Ltd','nooff':1000,
'pprice':46.10,'cprice':79.20,'factor':0.005,}
dic2={'inserial':2,'dateoff':20200805,'scrip':'Tata Motors Ltd','nooff':150,
'pprice':144.30,'cprice':298.40,'factor':0.01,}
dic3={'inserial':3,'dateoff':20210228,'scrip':'Reliance Industries Ltd','nooff':180,
'pprice':1912.00,'cprice':2424.00,'factor':0.015,}
dic4={'inserial':4,'dateoff':20210803,'scrip':'Hinustan Computers Ltd','nooff':55,
'pprice':1024.25,'cprice':1198.25,'factor':0.012,}
dic5={'inserial':5,'dateoff':20200905,'scrip':'Tech Mahindra Ltd','nooff':201,
'pprice':765.00,'cprice':1431.00,'factor':0.008,}

def leap(Y):
    if Y%400==0 or Y%100!=0 and Y%4==0:
        return True
    else:
        return False

def ytod(date):
    dt=str(date)
    dd=int(dt[-2:])
    mm=int(dt[-4:-2])
    Y=int(dt[0:4])
    A=[31,28,31,30,31,30,31,31,30,31,30,31]
    if leap(Y):
        A[1]=29
    else:
        A[1]=28
    sum=0
    for i in range(mm-1):
        sum+=A[i]
    return sum+dd

def yeartoadd(Y1,Y2):
    y1=int(str(Y1)[0:4])
    y2=int(str(Y2)[0:4])
    sum=0
    for y in range(y1,y2):
        if leap(y):
            sum+=366
        else:
            sum+=365
    return sum

def dif_days(Y1,Y2):
    d1=ytod(Y1)
    d2=ytod(Y2)
    y365=yeartoadd(Y1,Y2)
    return y365+d2-d1

def val(dic,code='c'):
    if code=='c':
        return dic['cprice']*dic['nooff']
    if code=='p':
        return dic['pprice']*dic['nooff']


list=[dic1,dic2,dic3,dic4,dic5]

def _str(list):
    print("{:^10s} {:3s}{:<25s} {:>5s} {:>10s} {:>10s} {:>10s} {:>10s} {:4s} {:^9s}".format('date','sl.','scrip',
        'nooff','purch','current','invest','current','days','Annualizd'))
    print("{:^10s} {:3s}{:<25s} {:5s} {:>10s} {:>10s} {:>10s} {:>10s} {:4s} {:>9s}".format('','','',
        ' ','price','price','value','value','','gain'))
    for dic in list:
        dt=str(dic['dateoff'])
        dd=dt[-2:]
        mm=dt[-4:-2]
        yy=dt[0:4]
        print("{}-{}-{} {:2d} {:<25s}. {:4d} {:10.2f} {:10.2f} {:10.2f} {:10.2f} {:4d} {:8.2f}%".format(dd,mm,yy,dic['inserial'],dic['scrip'],
        dic['nooff'],dic['pprice'],dic['cprice'],val(dic,'p'),val(dic,'c'),dif_days(dic['dateoff'],cdate),cumgain(dic)))
pass

def compin(a,b):
    return a['inserial']<b['inserial']
def compdate(a,b):
    return str(a['dateoff'])+str(a['inserial'])<str(b['dateoff'])+str(b['inserial'])
def compscrip(a,b):
    return a['scrip']<b['scrip']
def comppvalue(a,b):
    return a['pprice']*a['nooff']<b['pprice']*b['nooff']
def compcvalue(a,b):
    return a['cprice']*a['nooff']<b['cprice']*b['nooff']
def compgain(a,b):
    return cumgain(a)>cumgain(b)

def insert(L,i,j):
    tmp=L[j]
    for k in range(j,i,-1):
        L[k]=L[k-1]
    L[i]=tmp
    pass
def inssort(list,comp=compin):
    for i in range(len(list)):
        for j in range(i+1,len(list)):
            if comp(list[j],list[i]):
                insert(list,i,j)


cdate='11092021'        # input("curdate ddmmyyyy?")
cdate=cdate[-4:]+cdate[2:4]+cdate[0:2]

def cumgain(dic):
    pval=val(dic,'p')
    cval=val(dic,'c')
    dif=dif_days(dic['dateoff'],cdate)
    percent=(cval-pval)/dif*365/pval*100
    return percent


inssort(list)
_str(list)
p='1'
if (0):
    exit()
while (p<'9'):
    instr="input sortkey ?\n1 on dateoff\n2 on Scrip name\n3 on puchase value\n4 on current value\n5 on Annualized gain (reverse)\n9 exit\n"
    print(cdate)
    p=input(instr)
    comp=compin
    if p=='1' :
        comp=compdate
    if p=='2' :
        comp=compscrip
    if p=='3' :
        comp=comppvalue
    if p=='4' :
        comp=compcvalue
    if p=='5' :
        comp=compgain
    if p=='9' :
        exit()
    inssort(list,comp)
    _str(list)

#### Sample output
# 15:24 public_html/bnvenkat.com/apy2> python3 shareapp.py
#    date    sl.scrip                     nooff      purch    current     invest    current days Annualizd
#                                                    price      price      value      value           gain
# 05-08-2020  1 Bank of Baroda Ltd       . 1000      46.10      79.20   46100.00   79200.00  402    65.19%
# 05-08-2020  2 Tata Motors Ltd          .  150     144.30     298.40   21645.00   44760.00  402    96.96%
# 28-02-2021  3 Reliance Industries Ltd  .  180    1912.00    2424.00  344160.00  436320.00  195    50.12%
# 03-08-2021  4 Hinustan Computers Ltd   .   55    1024.25    1198.25   56333.75   65903.75   39   158.99%
# 05-09-2020  5 Tech Mahindra Ltd        .  201     765.00    1431.00  153765.00  287631.00  371    85.65%
# 20210911
# input sortkey ?
# 1 on dateoff
# 2 on Scrip name
# 3 on puchase value
# 4 on current value
# 5 on Annualized gain (reverse)
# 9 exit
# 2
#    date    sl.scrip                     nooff      purch    current     invest    current days Annualizd
#                                                    price      price      value      value           gain
# 05-08-2020  1 Bank of Baroda Ltd       . 1000      46.10      79.20   46100.00   79200.00  402    65.19%
# 03-08-2021  4 Hinustan Computers Ltd   .   55    1024.25    1198.25   56333.75   65903.75   39   158.99%
# 28-02-2021  3 Reliance Industries Ltd  .  180    1912.00    2424.00  344160.00  436320.00  195    50.12%
# 05-08-2020  2 Tata Motors Ltd          .  150     144.30     298.40   21645.00   44760.00  402    96.96%
# 05-09-2020  5 Tech Mahindra Ltd        .  201     765.00    1431.00  153765.00  287631.00  371    85.65%
# 20210911
# input sortkey ?
# 1 on dateoff
# 2 on Scrip name
# 3 on puchase value
# 4 on current value
# 5 on Annualized gain (reverse)
# 9 exit
# 5
#    date    sl.scrip                     nooff      purch    current     invest    current days Annualizd
#                                                    price      price      value      value           gain
# 03-08-2021  4 Hinustan Computers Ltd   .   55    1024.25    1198.25   56333.75   65903.75   39   158.99%
# 05-08-2020  2 Tata Motors Ltd          .  150     144.30     298.40   21645.00   44760.00  402    96.96%
# 05-09-2020  5 Tech Mahindra Ltd        .  201     765.00    1431.00  153765.00  287631.00  371    85.65%
# 05-08-2020  1 Bank of Baroda Ltd       . 1000      46.10      79.20   46100.00   79200.00  402    65.19%
# 28-02-2021  3 Reliance Industries Ltd  .  180    1912.00    2424.00  344160.00  436320.00  195    50.12%
# 20210911
# input sortkey ?
# 1 on dateoff
# 2 on Scrip name
# 3 on puchase value
# 4 on current value
# 5 on Annualized gain (reverse)
# 9 exit
# 4
#    date    sl.scrip                     nooff      purch    current     invest    current days Annualizd
#                                                    price      price      value      value           gain
# 05-08-2020  2 Tata Motors Ltd          .  150     144.30     298.40   21645.00   44760.00  402    96.96%
# 03-08-2021  4 Hinustan Computers Ltd   .   55    1024.25    1198.25   56333.75   65903.75   39   158.99%
# 05-08-2020  1 Bank of Baroda Ltd       . 1000      46.10      79.20   46100.00   79200.00  402    65.19%
# 05-09-2020  5 Tech Mahindra Ltd        .  201     765.00    1431.00  153765.00  287631.00  371    85.65%
# 28-02-2021  3 Reliance Industries Ltd  .  180    1912.00    2424.00  344160.00  436320.00  195    50.12%
# 20210911
# input sortkey ?
# 1 on dateoff
# 2 on Scrip name
# 3 on puchase value
# 4 on current value
# 5 on Annualized gain (reverse)
# 9 exit
# 9
# 15:25 public_html/bnvenkat.com/apy2>