Git Product home page Git Product logo

phpsort-go's Introduction

phpsort-go

phpsort-go is a Go language port of the PHP sort function. It provides functionality similar to the sort function used in PHP.

Usage

Import the package and use the Sort function:

package main

import (
	"github.com/demouth/phpsort-go/v2"
)

func main() {
	strings := []string{
		"lemon",
		"apple",
		"0     ",
		" 1    ",
		"  2   ",
		"   3  ",
		"   04 ",
		"    05",
	}

	phpsort.Sort(strings)

	for _, s := range strings {
		println(s)
	}
}

When using the comparison function:

package main

import (
	"github.com/demouth/phpsort-go/v2"
)

func main() {
	println(phpsort.ZendiSmartStrcmp("2", "1"))    // 1
	println(phpsort.ZendiSmartStrcmp("1", "2"))    // -1
	println(phpsort.ZendiSmartStrcmp("  10", "2")) // 1
	println(phpsort.ZendiSmartStrcmp("  1", "1"))  // 0
	println(phpsort.ZendiSmartStrcmp("A", "1"))    // 1
}

Behavior of the sort function in PHP

For reference, here's the original sort function in PHP:

<?php

$a = [
    'lemon',
    'apple',
    '0     ',
    ' 1    ',
    '  2   ',
    '   3  ',
    '   04 ',
    '    05',
];
sort($a);
var_dump($a);
/*
array(8) {
  [0]=>
  string(6) "0     "
  [1]=>
  string(6) " 1    "
  [2]=>
  string(6) "  2   "
  [3]=>
  string(6) "   3  "
  [4]=>
  string(6) "   04 "
  [5]=>
  string(6) "    05"
  [6]=>
  string(5) "apple"
  [7]=>
  string(5) "lemon"
}
*/

PHP7 Mode

Sorting results may differ between PHP7 and PHP8. This is due to the implementation of the following two RFCs:

By default, phpsort-go performs sorting compliant with PHP8. However, by specifying WithPHP7Mode, you can perform sorting that reproduces PHP7 behavior.

Here's an example of how to use it:

package main

import (
	"github.com/demouth/phpsort-go/v2"
)

func main() {
	strings := []string{
		"1.0",
		" 1",
		"1 ",
		"+1.0",
	}

	phpsort.Sort(strings, phpsort.WithPHP7Mode())

	for _, s := range strings {
		println(s)
	}
	// Using `WithPHP7Mode` will produce the following results:
	//   "1.0"
	//   " 1"
	//   "+1.0"
	//   "1 "
	// Note that without WithPHP7Mode, the following results will be produced:
	//   "1.0",
	//   " 1",
	//   "1 ",
	//   "+1.0",
}

phpsort-go's People

Contributors

demouth avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.