Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Add a feature in CsvSchema to allow definition of ordering #42

Closed
cowtowncoder opened this issue May 16, 2014 · 2 comments
Closed

Add a feature in CsvSchema to allow definition of ordering #42

cowtowncoder opened this issue May 16, 2014 · 2 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

(note: cleaved off of #40)


Currently the only way to specify ordering of columns with CsvSchema is to either use @JsonPropertyOrder annotation in POJO class, or to manually build schema.
But there is no way to indicate alternate ordering, explicit (by listing possibly partial list of columns) or otherwise (defining a strategy to use for sorting).
But there should be a way: for example, by having method(s) in CsvSchema to allow re-ordering of columns.

@cowtowncoder
Copy link
Member Author

Added CsvSchema.sortedBy() methods, matching tests.

@ShijunK
Copy link

ShijunK commented Oct 27, 2015

It seems the implementation only sorts the columns in headers, not actual values.

I have a test for 2.6.3

   public void testValueReorderByName() throws Exception
    {
        CsvMapper mapper = mapperForCsv();
        CsvSchema schema = mapper.schemaFor(Mixed.class);
        assertEquals(aposToQuotes("['a','b','c','d']"), schema.getColumnDesc());
        schema = schema.sortedBy("b", "c");
        assertEquals(aposToQuotes("['b','c','a','d']"), schema.getColumnDesc());

        Mixed value = new Mixed();
        value.a = 1;
        value.b = 2;
        value.c = 3;
        value.d = 4;

        schema = schema.withHeader();
        String outputString = mapper.writer(schema).writeValueAsString(Arrays.asList(value));
        assertThat(outputString, is(equalTo("b,c,a,d\n2,3,1,4\n")));

        _verifyLinks(schema);
    }

test output:

java.lang.AssertionError: 
Expected: is "b,c,a,d\n2,3,1,4\n"
     but: was "b,c,a,d\n1,2,3,4\n"

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

No branches or pull requests

2 participants