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

SortedBy only apply to headers and actual data #96

Closed
ShijunK opened this issue Oct 28, 2015 · 4 comments
Closed

SortedBy only apply to headers and actual data #96

ShijunK opened this issue Oct 28, 2015 · 4 comments
Milestone

Comments

@ShijunK
Copy link

ShijunK commented Oct 28, 2015

Follow-up to #42

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"
@jspo
Copy link

jspo commented Nov 26, 2015

I've found the same issue. The reason is that the method sortedBy only sorts the Column objects in the schema list but doesn't change the column objects. The Column objects of the list contain the original values of the _index and the _next attributes.

@cowtowncoder
Copy link
Member

Yes, I can reproduce this with given test. Will try to see how to fix.

@ShijunK
Copy link
Author

ShijunK commented Jan 14, 2016

@cowtowncoder great, thanks

@cowtowncoder
Copy link
Member

Fix should be in 2.7.0 which was released couple of days ago. 2.6.5 will be released within week or two as well.

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

3 participants