public URLNormalizer sortQueryParameters() {
        // Does it have query parameters?
        if (!url.contains("?")) {
            return this;
        }
        // It does, so proceed
        List<String> keyValues = new ArrayList<>();
        String queryString = StringUtils.substringAfter(url, "?");

        // extract and remove any fragments
        String fragment = StringUtils.substringAfter(queryString, "#");
        if (StringUtils.isNotBlank(fragment)) {
            fragment = "#" + fragment;
        }
        queryString = StringUtils.substringBefore(queryString, "#");

        String[] params = StringUtils.split(queryString, '&');
        for (String param : params) {
            keyValues.add(param);
        }
        // sort it so that query string are in order
        Collections.sort(keyValues);

        String sortedQueryString = StringUtils.join(keyValues, '&');
        if (StringUtils.isNotBlank(sortedQueryString)) {
            url = StringUtils.substringBefore(
                    url, "?") + "?" + sortedQueryString + fragment;
        }

        return this;
    }
@Test
    public void testSortQueryParameters() {
        // test with fragment
        s = "http://example.com?z=1&a=1#frag";
        t = "http://example.com?a=1&z=1#frag";
        assertEquals(t, n(s).sortQueryParameters().toString());
        // test duplicate params
        s = "http://example.com?z=1&a=1&a=1";
        t = "http://example.com?a=1&a=1&z=1";
        assertEquals(t, n(s).sortQueryParameters().toString());
        s = "http://www.example.com/display?lang=en&article=fred";
        t = "http://www.example.com/display?article=fred&lang=en";
        assertEquals(t, n(s).sortQueryParameters().toString());
        s = "http://www.example.com/?z=bb&y=cc&z=aa";
        t = "http://www.example.com/?y=cc&z=aa&z=bb";
        assertEquals(t, n(s).sortQueryParameters().toString());
        // Sorting should not change encoding
        s = "http://www.example.com/spa ce?z=b%2Fb&y=c c&z=a/a";
        t = "http://www.example.com/spa ce?y=c c&z=a/a&z=b%2Fb";
        assertEquals(t, n(s).sortQueryParameters().toString());
        s = "http://www.example.com/?z&y=c c&y=c c&a&d=&";
        t = "http://www.example.com/?a&d=&y=c c&y=c c&z";
        assertEquals(t, n(s).sortQueryParameters().toString());
    }