I am trying to sort an array users that have don't have the InvitesApplication.createdAt -> the user that have lastest createdAt in this team.
Here is my code sand box for an example: https://codesandbox.io/s/jovial-pasteur-seusw?file=/src/App.js
Update add code into question:
export default function App() {
const [thisTeamId, setThisTeamId] = useState(3);
const users = [
{
InvitesApplications: [
{
teamId: 1,
response: "Waiting on response",
createdAt: "2021-05-08T10:02:40.340Z"
}
],
Memberships: [{ teamId: 2 }],
fullname: "Nathan"
},
{
InvitesApplications: [
{
teamId: 2,
response: "Waiting on response",
createdAt: "2021-05-08T10:41:35.921Z"
}
],
Memberships: [{ teamId: 1 }],
fullname: "Nick"
},
{
InvitesApplications: [
{
teamId: 2,
response: "Waiting on response",
createdAt: "2021-05-08T05:38:51.554Z"
},
{
teamId: 1,
response: "Waiting on response",
createdAt: "2021-05-09T02:57:13.047Z"
}
],
Memberships: [],
fullname: "Nancy"
},
{
InvitesApplications: [],
Memberships: [],
fullname: "PPP"
}
];
return (
<div className="App">
{users
.filter(
(user) =>
user.Memberships.length < 1 ||
user.Memberships.every((member) => member.teamId !== thisTeamId)
)
.sort(
(a, b) =>
new Date(b.InvitesApplications[0]?.createdAt) -
new Date(a.InvitesApplications[0]?.createdAt)
)
.map((user, index) => (
<div key={index}>
<InviteCard user={user} thisTeamId={thisTeamId} />
</div>
))}
</div>
);
}
How can I implement my sort function? It didn't show me the user that dont have have Invitations.createdAt first