Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#762 : fix float number sorting #763

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions __test__/sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,19 @@ describe('Sort', function () {
expect(list.items[5].values().val).toBe('my.string_41299.tif')
})
it('should show order of sorted floates (a bit wrong)', function () {
i1.values({ val: '10.0401' })
i2.values({ val: '10.022' })
i3.values({ val: '10.021999' })
i4.values({ val: '11.231' })
i5.values({ val: '0003.123' })
i6.values({ val: '09.2123' })
i1.values({ val: '0.5' })
i2.values({ val: '0.26' })
i3.values({ val: '0.040' })
i4.values({ val: '0.50' })
i5.values({ val: '0.500' })
i6.values({ val: '0.3' })
list.sort('val', { order: 'asc' })
expect(list.items[0].values().val).toBe('0003.123')
expect(list.items[1].values().val).toBe('09.2123')
expect(list.items[2].values().val).toBe('10.022')
expect(list.items[3].values().val).toBe('10.0401')
expect(list.items[4].values().val).toBe('10.021999')
expect(list.items[5].values().val).toBe('11.231')
expect(list.items[0].values().val).toBe('0.040')
expect(list.items[1].values().val).toBe('0.26')
expect(list.items[2].values().val).toBe('0.3')
expect(list.items[3].values().val).toBe('0.5')
expect(list.items[4].values().val).toBe('0.50')
expect(list.items[5].values().val).toBe('0.500')
})
it('should sort IP addresses', function () {
i1.values({ val: '192.168.1.1' })
Expand Down
39 changes: 35 additions & 4 deletions src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,30 @@ module.exports = function (list) {
},
}

function isFloatString(value) {
if (value === undefined || value === null || !value || typeof value !== 'string' || value.split(".").length !== 2) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    value.split(".").length !== 2

--> just check value(string type) has only one . because of ip sorting test case.

return false
}

return !Number.isInteger(parseFloat(value))
}

function getFloatingPointLength(value) {
return value.split(".")[1].length
}

function getMaxFloatingPointLengthFromItems(itemA, itemB) {
const itemAFloatingNumberLength = getFloatingPointLength(itemA)
const itemBFloatingNumberLength = getFloatingPointLength(itemB)

return Math.max(itemAFloatingNumberLength, itemBFloatingNumberLength)
}

var sort = function () {
list.trigger('sortStart')
var options = {}

var target = arguments[0].currentTarget || arguments[0].srcElement || undefined
var target = arguments[0].currentTarget || arguments[0].srcElement || undefined // arg[0] : val, arg[1] : { order: 'asc' }

if (target) {
options.valueName = list.utils.getAttribute(target, 'data-sort')
Expand All @@ -68,8 +87,8 @@ module.exports = function (list) {
// caseInsensitive
// alphabet
var customSortFunction = options.sortFunction || list.sortFunction || null,
multi = options.order === 'desc' ? -1 : 1,
sortFunction
multi = options.order === 'desc' ? -1 : 1,
sortFunction

if (customSortFunction) {
sortFunction = function (itemA, itemB) {
Expand All @@ -82,7 +101,19 @@ module.exports = function (list) {
if (!sort.alphabet && options.insensitive) {
sort = list.utils.naturalSort.caseInsensitive
}
return sort(itemA.values()[options.valueName], itemB.values()[options.valueName]) * multi

const itemAValue = itemA.values()[options.valueName]
const itemBValue = itemB.values()[options.valueName]
if (isFloatString(itemAValue) && isFloatString(itemBValue)) {
const multiplier = 10 ** getMaxFloatingPointLengthFromItems(itemAValue, itemBValue) // multiply by max length for making float to integer

const valueA = (parseInt(parseFloat(itemAValue) * multiplier)).toString()
const valueB = (parseInt(parseFloat(itemBValue) * multiplier)).toString()

return sort(valueA, valueB) * multi
}

return sort(itemAValue, itemBValue) * multi
}
}

Expand Down