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

Table sorting by text length or alphabetically? #1792

Closed
kvetoslavnovak opened this issue May 20, 2016 · 10 comments
Closed

Table sorting by text length or alphabetically? #1792

kvetoslavnovak opened this issue May 20, 2016 · 10 comments

Comments

@kvetoslavnovak
Copy link
Contributor

The table sorter is sorting text strings by length not alphabetically. I do not know how is this in Chinese language but in euro american countries sorting of text is done alphabetically.

@kvetoslavnovak kvetoslavnovak changed the title Table sorting by text length or alphabet? Table sorting by text length or alphabetically? May 20, 2016
@afc163
Copy link
Member

afc163 commented May 20, 2016

[{
  title: 'age',
  dataIndex: 'age',
  sorter: (a, b) => a.age - b.age,
}]

You can specify your own sort algorithm. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

@afc163 afc163 closed this as completed May 20, 2016
@KirillYoYo
Copy link

KirillYoYo commented Jun 21, 2017

for sort by alpgavite:
function compareByAlph (a, b) { if (a > b) { return -1; } if (a < b) { return 1; } return 0; } ... sorter: (a, b) => compareByAlph(a.lastName, b.lastName),

@Leong21
Copy link

Leong21 commented Aug 16, 2017

It would be better to use JavaScript String#localeCompare function:

sorter: (a, b) => { return a.title.localeCompare(b.title)},

@tzs007
Copy link

tzs007 commented Jan 4, 2018

thx @Leong21 for this clever solution

@ghoshnirmalya
Copy link

ghoshnirmalya commented Jun 21, 2018

I'm still unable to figure out how can we sort columns using Dates. I'm doing something like:

        sorter: (a, b) => {
          let dateA = !a.publishedAt
            ? new Date().getTime()
            : new Date(a.publishedAt).getTime();

          let dateB = !b.publishedAt
            ? new Date().getTime()
            : new Date(b.publishedAt).getTime();

          return [dateA, dateB].sort();
        }

But this isn't working. publishedAt can be either "" or some date like "21 May, 2017" or "21 May, 2017 3:05 PM".

@Leong21
Copy link

Leong21 commented Jun 22, 2018

@ghoshnirmalya try using moment.js if possible.

sorter: (a, b) => { return moment(a.publishedAt).unix() - moment(b.publishedAt).unix()}

in order to handle the situation where publishedAt == "" try:
sorter: (a, b) => { return moment(a.publishedAt || 0).unix() - moment(b.publishedAt || 0).unix() }

@ghoshnirmalya
Copy link

@Leong21 Thank you! That works as expected.

@rizwanbinnawaz
Copy link

Good work @ leong21
actually The localeCompare() method compares two strings in the current locale.
and it would be better
sorter: (a, b) => { return a.title.localeCompare(b.title)},

@dcantu96
Copy link

@Leong21 Thank you! This should be explained or mentioned here:https://vue.ant.design/components/table/

@atayahmet
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants