@Override
  public ExampleSet apply(ExampleSet exampleSet) throws OperatorException {

    if (exampleSet == null) {
      throw new UserError(this, 149, getInputPorts().getPortByIndex(0).getName());
    }

    // get unmachted attributes
    Attributes attributes = exampleSet.getAttributes();
    List<Attribute> unmachtedAttributes = getAttributeList(attributes);

    if (getParameterAsString(PARAMETER_ORDER_MODE).equals(ALPHABETICALLY_MODE)) {

      if (getParameterAsString(PARAMETER_SORT_DIRECTION).equals(DIRECTION_NONE)) {
        return exampleSet;
      }

      // sort attributes
      sortAttributeListAlphabetically(unmachtedAttributes);

      // apply sorted attributes
      applySortedAttributes(unmachtedAttributes, null, attributes);

    } else if (getParameterAsString(PARAMETER_ORDER_MODE).equals(USER_SPECIFIED_RULES_MODE)
        || getParameterAsString(PARAMETER_ORDER_MODE).equals(REFERENCE_DATA)) {
      List<Attribute> sortedAttributes = new LinkedList<>();

      if (getParameterAsString(PARAMETER_ORDER_MODE).equals(REFERENCE_DATA)) {
        InputPort referencePort = getInputPorts().getPortByName(REFERENCE_DATA_PORT_NAME);
        ExampleSet referenceSet = referencePort.getData(ExampleSet.class);

        if (referenceSet == null) {
          throw new UserError(this, 149, referencePort.getName());
        }

        // iterate over reference attributes and order unmachted attributes accordingly
        for (Attribute refAttr : referenceSet.getAttributes()) {
          // System.out.println("Check attribute " + refAttr.getName());
          Iterator<Attribute> iterator = unmachtedAttributes.iterator();
          while (iterator.hasNext()) {
            Attribute unmachtedAttr = iterator.next();
            if (refAttr.getName().equals(unmachtedAttr.getName())) {

              // only pairwise matching is possible -> directly add attribute to
              // sorted list
              sortedAttributes.add(unmachtedAttr);
              // System.out.println("Added unmachted attribute to list: " +
              // unmachtedAttr.getName());

              // remove attribute from unmachted attributes
              iterator.remove();
            }
          }
        }
      } else {
        String combinedMaskedRules = getParameterAsString(PARAMETER_ORDER_RULES);
        if (combinedMaskedRules == null || combinedMaskedRules.length() == 0) {
          throw new UndefinedParameterError(PARAMETER_ORDER_RULES, this);
        }

        // iterate over all rules
        for (String maskedRule : combinedMaskedRules.split("\\|")) {
          String rule = Tools.unmask('|', maskedRule); // unmask them to allow regexp
          List<Attribute> matchedAttributes = new LinkedList<>();

          // iterate over all attributes and check if rules apply
          Iterator<Attribute> iterator = unmachtedAttributes.iterator();
          while (iterator.hasNext()) {
            Attribute attr = iterator.next();
            boolean match = false;
            if (getParameterAsBoolean(PARAMETER_USE_REGEXP)) {
              try {
                if (attr.getName().matches(rule)) {
                  match = true;
                }
              } catch (PatternSyntaxException e) {
                throw new UserError(this, 206, rule, e.getMessage());
              }
            } else {
              if (attr.getName().equals(rule)) {
                match = true;
              }
            }

            // if rule applies remove attribute from unmachted list and add it to rules
            // matched list
            if (match) {
              iterator.remove();
              matchedAttributes.add(attr);
            }
          }

          // sort matched attributes according to sort direction if more then one match
          // has been found
          if (matchedAttributes.size() > 1) {
            sortAttributeListAlphabetically(matchedAttributes);
          }

          // add matched attributes to sorted attribute list
          sortedAttributes.addAll(matchedAttributes);
        }
      }

      /*
       * UNMACHTED Handling
       */

      if (!getParameterAsString(PARAMETER_HANDLE_UNMATCHED_ATTRIBUTES)
          .equals(REMOVE_UNMATCHED_MODE)) {
        // sort unmachted attributes according to sort direction
        sortAttributeListAlphabetically(unmachtedAttributes);

        if (getParameterAsString(PARAMETER_HANDLE_UNMATCHED_ATTRIBUTES)
            .equals(PREPEND_UNMATCHED_MODE)) {
          // prepend attributes to ordered attributes list
          sortedAttributes.addAll(0, unmachtedAttributes);
        } else {
          // append attributes to ordered attributes list
          sortedAttributes.addAll(unmachtedAttributes);
        }

        applySortedAttributes(sortedAttributes, null, attributes);

      } else {
        applySortedAttributes(sortedAttributes, unmachtedAttributes, attributes);
      }

    } else {
      throw new IllegalArgumentException(
          "Order mode " + getParameterAsString(PARAMETER_ORDER_MODE) + " is not implemented!");
    }
    return exampleSet;
  }