Skip to content

export is.sorted #2325

Open
Open

Description

@franknarf1

I'd like to test whether my input table is sorted and error out if not. The base tools for this are not very appealing.

For now, I have a workaround:

library(data.table)
is_sorted = function(DT, cols = names(DT)){
  e = as.call(lapply(c("order", cols), as.name))
  identical(seq_len(nrow(DT)), DT[eval(e), which = TRUE])
} 

DT = data.table(id = 1:3, v = c(2,1,3))
is_sorted(DT) # TRUE
is_sorted(DT, "v") # FALSE

This is similar to a couple answers on SO, but implicitly uses forder instead of order. I guess data.table:::is.sorted is more efficient for this, though.

Activity

  1. MichaelChirico commented on Sep 6, 2017

    @MichaelChirico
    Member

    Doesn't is.unsorted use the same sorting algorithm that data.table does now?

    https://stackoverflow.com/q/31774807/3576984

  2. added this to the 1.12.4 milestone on Mar 8, 2019
  3. mattdowle commented on May 23, 2019

    @mattdowle
    Member

    @MichaelChirico It's very different.

    Note that ?base::is.unsorted contains "This function is designed for objects with one-dimensional indices, as described above. Data frames, matrices and other arrays may give surprising results.". IIRC, it pastes together the character representation of the rows and then compares those strings. Accordingly, a huge number of long strings are created and hence is very slow. I'm not sure what the surprising cases are but I think it might involve at least user options like scipen.

    In contrast, data.table::is.sorted has a columnar algorithm to do it, and is aware of integer64 too for example. It's much faster for data.frame|table (10x + ?) and actually correct.

    For straightforward vectors they're pretty much the same. After all, it's just iterating if < when a single vector (there is no sorting algorithm needed).

    Agree with exporting. Just need some documentation. My comments here could be basically it.

    (It's also fairly easily to parallize. Just needs to ensure each chunk overlaps the previous one by 1-row, and return as soon as any chunk finds unsorted.)

  4. changed the title [-][Request] export is.sorted[/-] [+]export is.sorted[/+] on May 23, 2019
  5. modified the milestones: 1.12.4, 1.13.0 on Jul 25, 2019
  6. modified the milestones: 1.12.7, 1.12.9 on Dec 8, 2019
  7. self-assigned this
    on Apr 10, 2020
  8. jangorecki commented on Apr 11, 2020

    @jangorecki
    Member

    Is it safe to assume that is.sorted(list()) or is.sorted(data.table()) to return TRUE?
    And what about is.sorted(NULL)? Returning NA for NULL doesn't seem to be bad idea IMO.

  9. modified the milestones: 1.13.1, 1.13.3 on Oct 17, 2020
  10. modified the milestones: 1.14.3, on Jul 19, 2022
  11. modified the milestones: , 1.15.1 on Oct 29, 2023
  12. modified the milestones: 1.16.0, 1.17.0 on Jul 10, 2024
  13. modified the milestones: 1.17.0, 1.18.0 on Dec 9, 2024
  14. removed their assignment
    on Jul 9, 2025
  15. modified the milestones: 1.18.0, 1.19.0 on Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions