MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / sortIndex

Method sortIndex

src/danfojs-base/core/frame.ts:3059–3091  ·  view source on GitHub ↗
(options?:
        {
            inplace?: boolean
            ascending?: boolean
        }
    )

Source from the content-addressed store, hash-verified

3057 }
3058 ): DataFrame
3059 sortIndex(options?:
3060 {
3061 inplace?: boolean
3062 ascending?: boolean
3063 }
3064 ): DataFrame | void {
3065 const { ascending, inplace } = { ascending: true, inplace: false, ...options }
3066
3067 const indexPosition = utils.range(0, this.index.length - 1)
3068 const index = [...this.index]
3069
3070 const objToSort = index.map((idx, i) => {
3071 return { index: indexPosition[i], value: idx }
3072 })
3073
3074 const sortedObjectArr = utils.sortObj(objToSort, ascending)
3075 let sortedIndex = sortedObjectArr.map((obj) => obj.index);
3076 const newData = sortedIndex.map(i => (this.values as ArrayType2D)[i as number])
3077 sortedIndex = sortedIndex.map((i) => index[i as number]);
3078
3079 if (inplace) {
3080 this.$setValues(newData)
3081 this.$setIndex(sortedIndex)
3082 } else {
3083 return new DataFrame(newData, {
3084 index: sortedIndex,
3085 columns: [...this.columns],
3086 dtypes: [...this.dtypes],
3087 config: { ...this.config }
3088 })
3089 }
3090
3091 }
3092
3093 /**
3094 * Add new rows at the end of the DataFrame.

Callers

nothing calls this directly

Calls 5

sortObjMethod · 0.80
$setValuesMethod · 0.80
mapMethod · 0.65
$setIndexMethod · 0.65
rangeMethod · 0.45

Tested by

no test coverage detected