github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/container/sort_map/sort_map.go (about)

     1  package sort_map
     2  
     3  import (
     4  	"sort"
     5  )
     6  
     7  type IntIntKVPair struct {
     8  	Key   int64
     9  	Value int64
    10  }
    11  type IntStringKVPair struct {
    12  	Key   int64
    13  	Value string
    14  }
    15  type StringStringKVPair struct {
    16  	Key   string
    17  	Value string
    18  }
    19  type StringIntKVPair struct {
    20  	Key   string
    21  	Value int64
    22  }
    23  
    24  type IntIntKVPairList []IntIntKVPair
    25  type IntStringKVPairList []IntStringKVPair
    26  type StringStringKVPairList []StringStringKVPair
    27  type StringIntKVPairList []StringIntKVPair
    28  
    29  type KVPairSortList struct {
    30  	IntIntKVPairList
    31  	IntStringKVPairList
    32  	StringStringKVPairList
    33  	StringIntKVPairList
    34  
    35  	KVType string // "int|int", "string|string", "int|string", "string|int"
    36  	SortBy string // "key", "val", "key asc", "val asc", "key desc", "val desc",
    37  }
    38  
    39  func (p KVPairSortList) Swap(i, j int) {
    40  	switch p.KVType {
    41  	case "int|int":
    42  		p.IntIntKVPairList[i], p.IntIntKVPairList[j] = p.IntIntKVPairList[j], p.IntIntKVPairList[i]
    43  	case "int|string":
    44  		p.IntStringKVPairList[i], p.IntStringKVPairList[j] = p.IntStringKVPairList[j], p.IntStringKVPairList[i]
    45  	case "string|string":
    46  		p.StringStringKVPairList[i], p.StringStringKVPairList[j] = p.StringStringKVPairList[j], p.StringStringKVPairList[i]
    47  	case "string|int":
    48  		p.StringIntKVPairList[i], p.StringIntKVPairList[j] = p.StringIntKVPairList[j], p.StringIntKVPairList[i]
    49  	}
    50  }
    51  
    52  func (p KVPairSortList) Len() int {
    53  	switch p.KVType {
    54  	case "int|int":
    55  		return len(p.IntIntKVPairList)
    56  	case "int|string":
    57  		return len(p.IntStringKVPairList)
    58  	case "string|string":
    59  		return len(p.StringStringKVPairList)
    60  	case "string|int":
    61  		return len(p.StringIntKVPairList)
    62  	}
    63  	return 0
    64  }
    65  
    66  func (p KVPairSortList) Less(i, j int) bool {
    67  	switch p.KVType {
    68  	case "int|int":
    69  		return p.intIntVKLess(i, j)
    70  	case "int|string":
    71  		return p.intStringVKLess(i, j)
    72  	case "string|string":
    73  		return p.stringStringVKLess(i, j)
    74  	case "string|int":
    75  		return p.stringIntVKLess(i, j)
    76  	}
    77  
    78  	return false
    79  }
    80  
    81  func (p KVPairSortList) intIntVKLess(i, j int) bool {
    82  	if p.SortBy == "val" || p.SortBy == "val asc" {
    83  		return p.IntIntKVPairList[i].Value < p.IntIntKVPairList[j].Value
    84  	}
    85  	if p.SortBy == "val desc" {
    86  		return p.IntIntKVPairList[i].Value > p.IntIntKVPairList[j].Value
    87  	}
    88  	if p.SortBy == "key" || p.SortBy == "key asc" {
    89  		return p.IntIntKVPairList[i].Key < p.IntIntKVPairList[j].Key
    90  	}
    91  	if p.SortBy == "key desc" {
    92  		return p.IntIntKVPairList[i].Key > p.IntIntKVPairList[j].Key
    93  	}
    94  
    95  	return false
    96  }
    97  
    98  func (p KVPairSortList) intStringVKLess(i, j int) bool {
    99  	if p.SortBy == "val" || p.SortBy == "val asc" {
   100  		return p.IntStringKVPairList[i].Value < p.IntStringKVPairList[j].Value
   101  	}
   102  	if p.SortBy == "val desc" {
   103  		return p.IntStringKVPairList[i].Value > p.IntStringKVPairList[j].Value
   104  	}
   105  	if p.SortBy == "key" || p.SortBy == "key asc" {
   106  		return p.IntStringKVPairList[i].Key < p.IntStringKVPairList[j].Key
   107  	}
   108  	if p.SortBy == "key desc" {
   109  		return p.IntStringKVPairList[i].Key > p.IntStringKVPairList[j].Key
   110  	}
   111  
   112  	return false
   113  }
   114  
   115  func (p KVPairSortList) stringStringVKLess(i, j int) bool {
   116  	if p.SortBy == "val" || p.SortBy == "val asc" {
   117  		return p.StringStringKVPairList[i].Value < p.StringStringKVPairList[j].Value
   118  	}
   119  	if p.SortBy == "val desc" {
   120  		return p.StringStringKVPairList[i].Value > p.StringStringKVPairList[j].Value
   121  	}
   122  	if p.SortBy == "key" || p.SortBy == "key asc" {
   123  		return p.StringStringKVPairList[i].Key < p.StringStringKVPairList[j].Key
   124  	}
   125  	if p.SortBy == "key desc" {
   126  		return p.StringStringKVPairList[i].Key > p.StringStringKVPairList[j].Key
   127  	}
   128  
   129  	return false
   130  }
   131  
   132  func (p KVPairSortList) stringIntVKLess(i, j int) bool {
   133  	if p.SortBy == "val" || p.SortBy == "val asc" {
   134  		return p.StringIntKVPairList[i].Value < p.StringIntKVPairList[j].Value
   135  	}
   136  	if p.SortBy == "val desc" {
   137  		return p.StringIntKVPairList[i].Value > p.StringIntKVPairList[j].Value
   138  	}
   139  	if p.SortBy == "key" || p.SortBy == "key asc" {
   140  		return p.StringIntKVPairList[i].Key < p.StringIntKVPairList[j].Key
   141  	}
   142  	if p.SortBy == "key desc" {
   143  		return p.StringIntKVPairList[i].Key > p.StringIntKVPairList[j].Key
   144  	}
   145  
   146  	return false
   147  }
   148  
   149  // map[int64]int64 value asc
   150  func SortIntIntMapByValue(m map[int64]int64) IntIntKVPairList {
   151  	return sortIntIntMap(m, "val")
   152  }
   153  
   154  // map[int64]int64 value desc
   155  func SortIntIntMapByValueDesc(m map[int64]int64) IntIntKVPairList {
   156  	return sortIntIntMap(m, "val desc")
   157  }
   158  
   159  // map[int64]int64 key
   160  func SortIntIntMapByKey(m map[int64]int64) IntIntKVPairList {
   161  	return sortIntIntMap(m, "key")
   162  }
   163  
   164  // map[int64]int64 key desc
   165  func SortIntIntMapByKeyDesc(m map[int64]int64) IntIntKVPairList {
   166  	return sortIntIntMap(m, "key desc")
   167  }
   168  
   169  func sortIntIntMap(m map[int64]int64, sortBy string) IntIntKVPairList {
   170  	kvPairSortList := KVPairSortList{}
   171  	kvPairSortList.KVType = "int|int"
   172  	kvPairSortList.SortBy = sortBy
   173  	kvPairSortList.IntIntKVPairList = make(IntIntKVPairList, len(m))
   174  
   175  	i := 0
   176  	for k, v := range m {
   177  		kvPairSortList.IntIntKVPairList[i] = IntIntKVPair{k, v}
   178  		i++
   179  	}
   180  
   181  	sort.Sort(kvPairSortList)
   182  
   183  	return kvPairSortList.IntIntKVPairList
   184  }
   185  
   186  // map[int64]string value asc
   187  func SortIntStringMapByValue(m map[int64]string) IntStringKVPairList {
   188  	return sortIntStringMap(m, "val")
   189  }
   190  
   191  // map[int64]string value Desc
   192  func SortIntStringMapByValueDesc(m map[int64]string) IntStringKVPairList {
   193  	return sortIntStringMap(m, "val desc")
   194  }
   195  
   196  // map[int64]string key asc
   197  func SortIntStringMapByKey(m map[int64]string) IntStringKVPairList {
   198  	return sortIntStringMap(m, "key")
   199  }
   200  
   201  // map[int64]string key Desc
   202  func SortIntStringMapByKeyDesc(m map[int64]string) IntStringKVPairList {
   203  	return sortIntStringMap(m, "key desc")
   204  }
   205  
   206  func sortIntStringMap(m map[int64]string, sortBy string) IntStringKVPairList {
   207  	kvPairSortList := &KVPairSortList{}
   208  	kvPairSortList.KVType = "string|int"
   209  	kvPairSortList.SortBy = sortBy
   210  	kvPairSortList.IntStringKVPairList = make(IntStringKVPairList, len(m))
   211  
   212  	i := 0
   213  	for k, v := range m {
   214  		kvPairSortList.IntStringKVPairList[i] = IntStringKVPair{k, v}
   215  		i++
   216  	}
   217  
   218  	sort.Sort(kvPairSortList)
   219  
   220  	return kvPairSortList.IntStringKVPairList
   221  }
   222  
   223  // map[string]string value Desc
   224  func SortStringStringMapByValue(m map[string]string) StringStringKVPairList {
   225  	return sortStringStringMap(m, "val")
   226  }
   227  
   228  // map[string]string value Desc
   229  func SortStringStringMapByValueDesc(m map[string]string) StringStringKVPairList {
   230  	return sortStringStringMap(m, "val desc")
   231  }
   232  
   233  // map[string]string key asc
   234  func SortStringStringMapByKey(m map[string]string) StringStringKVPairList {
   235  	return sortStringStringMap(m, "key")
   236  }
   237  
   238  // map[string]string key Desc
   239  func SortStringStringMapByKeyDesc(m map[string]string) StringStringKVPairList {
   240  	return sortStringStringMap(m, "key desc")
   241  }
   242  
   243  func sortStringStringMap(m map[string]string, sortBy string) StringStringKVPairList {
   244  	kvPairSortList := &KVPairSortList{}
   245  	kvPairSortList.KVType = "string|string"
   246  	kvPairSortList.SortBy = sortBy
   247  	kvPairSortList.StringStringKVPairList = make(StringStringKVPairList, len(m))
   248  
   249  	i := 0
   250  	for k, v := range m {
   251  		kvPairSortList.StringStringKVPairList[i] = StringStringKVPair{k, v}
   252  		i++
   253  	}
   254  
   255  	sort.Sort(kvPairSortList)
   256  
   257  	return kvPairSortList.StringStringKVPairList
   258  }
   259  
   260  // map[string]int64 value
   261  func SortStringIntMapByValue(m map[string]int64) StringIntKVPairList {
   262  	return sortStringIntMap(m, "val desc")
   263  }
   264  
   265  // map[string]int64 value desc
   266  func SortStringIntMapByValueDesc(m map[string]int64) StringIntKVPairList {
   267  	return sortStringIntMap(m, "val desc")
   268  }
   269  
   270  // map[string]int64 key asc
   271  func SortStringIntMapByKey(m map[string]int64) StringIntKVPairList {
   272  	return sortStringIntMap(m, "key")
   273  }
   274  
   275  // map[string]int64 key desc
   276  func SortStringIntMapByKeyDesc(m map[string]int64) StringIntKVPairList {
   277  	return sortStringIntMap(m, "key desc")
   278  }
   279  
   280  func sortStringIntMap(m map[string]int64, sortBy string) StringIntKVPairList {
   281  	kvPairSortList := &KVPairSortList{}
   282  	kvPairSortList.KVType = "string|int"
   283  	kvPairSortList.SortBy = sortBy
   284  	kvPairSortList.StringIntKVPairList = make(StringIntKVPairList, len(m))
   285  
   286  	i := 0
   287  	for k, v := range m {
   288  		kvPairSortList.StringIntKVPairList[i] = StringIntKVPair{k, v}
   289  		i++
   290  	}
   291  
   292  	sort.Sort(kvPairSortList)
   293  
   294  	return kvPairSortList.StringIntKVPairList
   295  }