code: private List<String> sortByPath(List<HttpCookie> cookies) {
          Collections.sort(cookies, new CookiePathComparator());
  
          final StringBuilder result = new StringBuilder();
  
          // Netscape cookie spec and RFC 2965 have different format of Cookie
          // header; RFC 2965 requires a leading $Version="1" string while Netscape
          // does not.
          // The workaround here is to add a $Version="1" string in advance
          int minVersion = 1;
          for (HttpCookie cookie : cookies) {
              if (cookie.getVersion() < minVersion) {
                  minVersion = cookie.getVersion();
              }
          }
  
          if (minVersion == 1) {
              result.append("$Version=\"1\"; ");
          }
  
          for (int i = 0; i < cookies.size(); ++i) {
              if (i != 0) {
                  result.append("; ");
              }
  
              result.append(cookies.get(i).toString());
          }
  
          List<String> cookieHeader = new java.util.ArrayList<String>();
          cookieHeader.add(result.toString());
          return cookieHeader;
      }
docs: /*
  sort cookies with respect to their path: those with more specific Path attributes
  precede those with less specific, as defined in RFC 2965 sec. 3.3.4