Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Sort by date in users app


Hi,

I'm trying to sort a column by date in the users app.

The data model that is used in Metronic looks like this:

_models.ts


import {ID, Response} from "../../../../../../_metronic/helpers"
export type User = {
id?: ID
name?: string
avatar?: string
email?: string
position?: string
role?: string
last_login?: string
two_steps?: boolean
joined_day?: string
online?: boolean
initials?: {
label: string
state: string
}
}

export type UsersQueryResponse = Response<Array<User>>

export const initialUser: User = {
avatar: "avatars/300-6.jpg",
position: "Art Director",
role: "Administrator",
name: "",
email: "",
}


I'm trying to add an additional field:


export type User = {
id?: ID
created?: Date
name?: string
avatar?: string
email?: string
position?: string
role?: string
last_login?: string
two_steps?: boolean
joined_day?: string
online?: boolean
initials?: {
label: string
state: string
}



And in my columns definition:

{
Header: (props) => (
<ResultCustomHeader tableProps={props} title='Created Date' className='min-w-125px' />
),
accessor: 'created',
sortType: 'datetime',
Cell: ({...props}) => <>{props.data[props.row.index].date.toString()}</>
},


When running this is doesn't sort them by date... it looks like it's sorting by something else, probably a string.

Can you please provide a working example?

Thanks!


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (5)


Thanks!



Are you using this technique?



Hi,

Yes in our datatable examples, we are using react-tables. In our Users Managment example we are handling sorting on server side, you can use the same approach and return sorted array by date to your client-side.

You can check our Swagger API example: https://preview.keenthemes.com/theme-api/api/documentation#/

Regards,
Lauris Stepanovs,
Keenthemes Support Team



Is there a reason you're not using "automatic sorting" (useSortBy) in the users app in Metronic?

Here's an example:

https://github.com/TanStack/table/tree/v7/examples/sorting



I've got the same issue with a field of type number:

age?: number

Can you please provide an example for this one as well?


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(